1 | public void testImmutableCollectionWithUpdate() {↵ | | 1 | public void testImmutableChildEntityWithMerge() {↵
|
2 | Contract c = new Contract("gavin", "phone");↵ | | 2 | Contract c = new Contract("gavin", "phone");↵
|
3 | ContractVariation cv1 = new ContractVariation(1, c);↵ | | 3 | ContractVariation cv1 = new ContractVariation(1, c);↵
|
4 | cv1.setText("expensive");↵ | | 4 | cv1.setText("expensive");↵
|
5 | ContractVariation cv2 = new ContractVariation(2, c);↵ | | 5 | ContractVariation cv2 = new ContractVariation(2, c);↵
|
6 | cv2.setText("more expensive");↵ | | 6 | cv2.setText("more expensive");↵
|
7 | Session s = openSession();↵ | | 7 | Session s = openSession();↵
|
8 | Transaction t = s.beginTransaction();↵ | | 8 | Transaction t = s.beginTransaction();↵
|
9 | s.persist(c);↵ | | 9 | s.persist(c);↵
|
10 | t.commit();↵ | | 10 | t.commit();↵
|
11 | s.close();↵ | | 11 | s.close();↵
|
|
12 | s = openSession();↵ | | 12 | s = openSession();↵
|
13 | t = s.beginTransaction();↵ | | 13 | t = s.beginTransaction();↵
|
14 | c.getVariations().add( new ContractVariation(3, c) );↵ | | 14 | c↵
|
15 | try {↵ | | |
|
16 | s.update( c );↵ | | |
|
17 | fail( "should have failed because reassociated object has a dirty collection");↵ | | |
|
18 | }↵ | | |
|
19 | catch ( HibernateException ex ) {↵ | | |
|
20 | // expected↵ | | |
|
21 | }↵ | | |
|
22 | finally {↵ | | 15 | v1 = (ContractVariation) c.getVariations().iterator().next();↵
|
| | | 16 | cv1.setText("blah blah");↵
|
| | | 17 | s.merge( c );↵
|
23 | t.rollback();↵ | | 18 | t.commit();↵
|
24 | s.close();↵ | | 19 | s.close();↵
|
25 | }↵ | | |
|
|
26 | s = openSession();↵ | | 20 | s = openSession();↵
|
27 | t = s.beginTransaction();↵ | | 21 | t = s.beginTransaction();↵
|
28 | c = (Contract) s.createCriteria(Contract.class).uniqueResult();↵ | | 22 | c = (Contract) s.createCriteria(Contract.class).uniqueResult();↵
|
29 | assertEquals( c.getCustomerName(), "gavin" );↵ | | 23 | assertEquals( c.getCustomerName(), "gavin" );↵
|
30 | assertEquals( c.getVariations().size(), 2 );↵ | | 24 | assertEquals( c.getVariations().size(), 2 );↵
|
31 | Iterator it = c.getVariations().iterator();↵ | | 25 | Iterator it = c.getVariations().iterator();↵
|
32 | cv1 = (ContractVariation) it.next();↵ | | 26 | cv1 = (ContractVariation) it.next();↵
|
33 | assertEquals( cv1.getText(), "expensive" );↵ | | 27 | assertEquals( cv1.getText(), "expensive" );↵
|
34 | cv2 = (ContractVariation) it.next();↵ | | 28 | cv2 = (ContractVariation) it.next();↵
|
35 | assertEquals( cv2.getText(), "more expensive" );↵ | | 29 | assertEquals( cv2.getText(), "more expensive" );↵
|
36 | s.delete(c);↵ | | 30 | s.delete(c);↵
|
37 | assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵ | | 31 | assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵
|
38 | assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵ | | 32 | assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵
|
39 | t.commit();↵ | | 33 | t.commit();↵
|
40 | s.close();↵ | | 34 | s.close();↵
|
41 | | | 35 |
|