Code example for Session

Methods: closebeginTransactioncreateCriteriagetTransaction

0
		s.close();
 
		sessionFactory().getStatistics().clear(); 
 
		s = openSession();
		s.beginTransaction();
		u = ( User ) s.createCriteria( User.class )
				.add( Restrictions.naturalId().set( "userName", "steve" ) )
				.setCacheable( true )
				.uniqueResult();
		assertNotNull( u );
		assertEquals( sessionFactory().getStatistics().getQueryExecutionCount(), 0 ); 
		assertEquals( sessionFactory().getStatistics().getQueryCacheHitCount(), 1 ); 
		u = ( User ) s.createCriteria( User.class )
				.add( Restrictions.naturalId().set( "userName", "steve" ) )
				.setCacheable( true )
				.uniqueResult();
		assertNotNull( u );
		assertEquals( sessionFactory().getStatistics().getQueryExecutionCount(), 0 ); 
		assertEquals( sessionFactory().getStatistics().getQueryCacheHitCount(), 2 ); 
		s.getTransaction().commit();
		s.close();
 
		s = openSession();
		s.beginTransaction();
		s.createQuery( "delete User" ).executeUpdate();
		s.getTransaction().commit();