List of usage examples for org.hibernate SessionFactory getCurrentSession
Session getCurrentSession() throws HibernateException;
From source file:uk.ac.ebi.emma.infrastructure.SpringHibernateConfigurationTest.java
License:Apache License
/** * Test of getSessionFactory method, of class HibernateUtils. *///from ww w . j a v a 2 s .co m @Test public void testGetSessionFactoryUsingDefaultProperties() { SessionFactory sessionFactory = HibernateUtils.getSessionFactory(); SessionFactory sessionFactory1 = HibernateUtils.getSessionFactory(); assertEquals(sessionFactory, sessionFactory1); Session session = sessionFactory.getCurrentSession(); assertNotNull(session); session = sessionFactory.getCurrentSession(); assertNotNull(session); logger.info("Got session. Fetching genes."); List<Gene> genes = getGenes(session); logger.info("Read " + genes.size() + " genes."); assertFalse(session.isOpen()); // Don't close the factory. Doing so causes subsequent factory requests to fail. // See http://stackoverflow.com/questions/11901130/org-springframework-orm-hibernate4-hibernatesystemexception-unknown-service-req // sessionFactory.close(); // assertTrue(sessionFactory.isClosed()); }
From source file:uk.ac.ebi.emma.infrastructure.SpringHibernateConfigurationTest.java
License:Apache License
/** * Test of getSessionFactory method, of class HibernateUtils. *//*from w w w . j ava 2 s .c om*/ @Test public void testGetSessionFactoryUsingCustomProperties() { Resource resource = new ClassPathResource("/hibernate.test.properties"); Properties hibernateProperties = null; try { hibernateProperties = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { fail("Unable to load hibernate.test.properties file."); } HibernateUtils.setHibernateProperties(hibernateProperties); SessionFactory sessionFactory = HibernateUtils.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); assertNotNull(session); session = sessionFactory.getCurrentSession(); assertNotNull(session); logger.info("Got session. Fetching genes."); List<Gene> genes = getGenes(session); logger.info("Read " + genes.size() + " genes."); assertFalse(session.isOpen()); // Don't close the factory. Doing so causes subsequent factory requests to fail. // See http://stackoverflow.com/questions/11901130/org-springframework-orm-hibernate4-hibernatesystemexception-unknown-service-req // sessionFactory.close(); // assertTrue(sessionFactory.isClosed()); }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
public BaseObject fetchSingleObject(String identifier) throws Exception { BaseObject object = null;//from ww w. j a v a 2s . co m SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); try { session.getTransaction().begin(); object = (BaseObject) session.get(factoryObject.getClass(), identifier); session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); session.getTransaction().rollback(); throw (new Exception("hibernate problem! " + e.getMessage())); } if (object == null) { throw (new Exception("No object with that identifier: " + identifier)); } return object; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
public BaseObject fetchSingleObject(Integer identifier) throws Exception { BaseObject object = null;/*from ww w .j a va 2 s .co m*/ SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); try { session.getTransaction().begin(); object = (BaseObject) session.get(factoryObject.getClass(), identifier); session.getTransaction().commit(); } catch (HibernateException e) { session.getTransaction().rollback(); throw (new Exception("hibernate problem! " + e.getMessage())); } if (object == null) { throw (new Exception("No object with that identifier")); } return object; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
public BaseObject fetchSingleObjectByRestrictions(RestrictionList restrictions) throws Exception { SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); BaseObject object = null;/*www.j a v a 2s. c om*/ try { session.getTransaction().begin(); Criteria crit = session.createCriteria(factoryObject.getClass()); for (Criterion c : restrictions) { crit.add(c); } object = (BaseObject) crit.uniqueResult(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw e; } return object; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public List<BaseObject> fetchManyObjects(RestrictionList restrictions) throws Exception { List<BaseObject> objects = null; SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); session.getTransaction().begin();/* w ww . ja va2 s . c om*/ Criteria crit = session.createCriteria(factoryObject.getClass()); for (Criterion c : restrictions) { crit.add(c); } try { objects = crit.list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw (e); } return objects; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public List<BaseObject> fetchManyObjects(RestrictionList restrictions, OrderList orders) throws Exception { List<BaseObject> objects = null; SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); try {/*from w ww. j a v a 2s . c o m*/ session.getTransaction().begin(); Criteria crit = session.createCriteria(factoryObject.getClass()); for (Criterion c : restrictions) { crit.add(c); } for (Order order : orders) { crit.addOrder(order); } objects = crit.list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw (e); } return objects; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public List<BaseObject> fetchManySubclassObjects(BaseObject subclassObject, RestrictionList restrictions) throws Exception { List<BaseObject> objects = null; SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); try {/* www . j a v a 2 s. co m*/ session.getTransaction().begin(); Criteria crit = session.createCriteria(subclassObject.getClass()); for (Criterion c : restrictions) { crit.add(c); } objects = crit.list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw e; } return objects; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public List<BaseObject> fetchManySubclassObjects(BaseObject subclassObject, RestrictionList restrictions, OrderList orders) throws Exception { List<BaseObject> objects = null; SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); try {//from w w w .j ava 2s . c o m session.getTransaction().begin(); Criteria crit = session.createCriteria(subclassObject.getClass()); for (Criterion c : restrictions) { crit.add(c); } for (Order order : orders) { crit.addOrder(order); } objects = crit.list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw e; } return objects; }
From source file:uk.ac.sussex.model.base.BaseFactory.java
License:Open Source License
public List<BaseObject> fetchManyByQuery(String query) throws Exception { List<BaseObject> objects = null; SessionFactory sessionFactory = SessionFactoryHelper.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); try {/*from w w w.j av a 2s . c o m*/ session.getTransaction().begin(); @SuppressWarnings("rawtypes") Iterator results = session.createQuery(query).iterate(); if (results != null) { objects = new ArrayList<BaseObject>(); while (results.hasNext()) { objects.add((BaseObject) results.next()); } } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw (e); } return objects; }