/** * */ package net.sf.l2j.gameserver.cache.sql; import java.util.logging.Logger; import net.sf.l2j.L2DatabaseFactory; import net.sf.l2j.gameserver.model.entity.Castle; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * @author zabbix * Lets drink to code! */ public class CacheD { protected static Logger _log = Logger.getLogger(CacheD.class.getName()); private static SessionFactory sessionFactory; public static void main(String args[]) { CacheD cache = new CacheD(); //cache.saveCastle(c); cache.loadAllCastles(); } public CacheD() { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("[CacheD] fatal: " + ex); throw new ExceptionInInitializerError(ex); } } private void pushObject(Object o) { Session sess = null; try { sess = sessionFactory.getCurrentSession(); sess.beginTransaction(); if(o instanceof DummyEntity) { DummyEntity e = (DummyEntity)o; sess.save(e); } sess.getTransaction().commit(); } catch(HibernateException e) { System.err.print("[CacheD] pushObject failed for "+o); if(sess != null && sess.getTransaction() != null) { sess.getTransaction().rollback(); System.err.print(", rollback..."); } System.err.println(""); e.printStackTrace(); } } public void loadAllCastles() { Castle c = null; Session sess = sessionFactory.getCurrentSession(); sess.beginTransaction(); c = (Castle)sess.load(Castle.class,1); sess.getTransaction().commit(); } public void saveCastle(Castle castle) { _log.info("[CacheD] saving data for castle "+castle.getName()); /** TODO: save manor crop procure data */ /** TODO: save manor seed production data */ pushObject(castle); } public Castle restoreCastleById(int id) { _log.info("[CacheD] restoring castle data for castleId "+id); Castle c = new Castle(id); return c; } }