Code example for Session

Methods: closedeletesavebeginTransactioncreateQuery

0
	public void testJoinedSubclass() { 
		Statistics statistics = sessionFactory().getStatistics();
		statistics.clear();
		 
		Session s = openSession();
		Transaction t = s.beginTransaction();
		 
		Salesperson mark = new Salesperson();
		mark.setName("Mark");
		mark.setTitle("internal sales");
		mark.setSex('M');
		mark.setAddress("buckhead");
		mark.setZip("30305");
		mark.setCountry("USA");
		 
		Person joe = new Person();
		joe.setName("Joe");
		joe.setAddress("San Francisco");
		joe.setZip("XXXXX");
		joe.setCountry("USA");
		joe.setSex('M');
		joe.setSalesperson(mark);
		mark.getCustomers().add(joe);
				 
		s.save(mark);
		 
		t.commit();
		 
		assertEquals( statistics.getEntityInsertCount(), 2 );
		assertEquals( statistics.getPrepareStatementCount(), 5 );
		 
		statistics.clear();
		 
		t = s.beginTransaction();
		s.delete(mark);
		t.commit();
 
		assertEquals( statistics.getEntityDeleteCount(), 2 );
		if ( getDialect().supportsCascadeDelete() ) { 
			assertEquals( statistics.getPrepareStatementCount(), 1 );
		} 
		 
		t = s.beginTransaction();
		List names = s.createQuery("select name from Person").list();
		assertTrue( names.isEmpty() );
		t.commit();
 
		s.close();
	} 
 
}