1 package org.hibernate.test.immutable; 3 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 import org.hibernate.criterion.Projections; 10 import org.hibernate.test.TestCase; 11 12 15 public class ImmutableTest extends TestCase { 16 17 public void testImmutable() { 18 Contract c = new Contract("gavin", "phone"); 19 ContractVariation cv1 = new ContractVariation(1, c); 20 cv1.setText("expensive"); 21 ContractVariation cv2 = new ContractVariation(2, c); 22 cv2.setText("more expensive"); 23 Session s = openSession(); 24 Transaction t = s.beginTransaction(); 25 s.persist(c); 26 t.commit(); 27 s.close(); 28 29 s = openSession(); 30 t = s.beginTransaction(); 31 c = (Contract) s.createCriteria(Contract.class).uniqueResult(); 32 c.setCustomerName("foo bar"); 33 c.getVariations().add( new ContractVariation(3, c) ); 34 cv1 = (ContractVariation) c.getVariations().iterator().next(); 35 cv1.setText("blah blah"); 36 t.commit(); 37 s.close(); 38 39 s = openSession(); 40 t = s.beginTransaction(); 41 c = (Contract) s.createCriteria(Contract.class).uniqueResult(); 42 assertEquals( c.getCustomerName(), "gavin" ); 43 assertEquals( c.getVariations().size(), 2 ); 44 cv1 = (ContractVariation) c.getVariations().iterator().next(); 45 assertEquals( cv1.getText(), "expensive" ); 46 s.delete(c); 47 assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer (0) ); 48 assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer (0) ); 49 t.commit(); 50 s.close(); 51 } 52 53 public ImmutableTest(String str) { 54 super(str); 55 } 56 57 protected String [] getMappings() { 58 return new String [] { "immutable/ContractVariation.hbm.xml" }; 59 } 60 61 public static Test suite() { 62 return new TestSuite(ImmutableTest.class); 63 } 64 65 } 66 67 | Popular Tags |