Code example for SessionFactory

Methods: openSession

0
	@Autowired 
	private SessionFactory sessionFactory;
 
	@Test
	public void retrieveAccount() { 
		Session session = sessionFactory.openSession(); // not part of a transaction, so we need to open a session manually
		Query query = session.createQuery("from Account a where a.id=:id").setInteger("id", 1);
		Account a = (Account) query.uniqueResult();
		session.close();
		Assert.assertEquals(a.getCashBalance(), 500.0, 0.01);
	} 
	 
	@Test @Transactional
	public void updateAccount() { 
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account a where a.id=:id").setInteger("id", 1);
		Account a = (Account) query.uniqueResult();
		a.setName("foo");
	}