1 | public void testImmutableChildEntityWithUpdate() {↵ | | 1 | public void testImmutableCollectionWithMerge() {↵
|
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 | cv1 = (ContractVariation) c.getVariations().iterator().nex↵ | | 14 | c.getVariations().add( new ContractVariation(3, c) );↵
|
| | | 15 | s.merge( c );↵
|
| | | 16 | try {↵
|
15 | t();↵ | | 17 | t.commit();↵
|
16 | cv1.setText("blah blah");↵ | | 18 | ↵
|
17 | s.update( c );↵ | | |
|
18 | t.commit();↵ | | |
|
| | | 19 | fail( "should have failed because an immutable collection was changed");↵
|
| | | 20 | }↵
|
| | | 21 | catch ( HibernateException ex ) {↵
|
| | | 22 | // expected↵
|
| | | 23 | t.rollback();↵
|
| | | 24 | }↵
|
| | | 25 | finally {↵
|
19 | s.close();↵ | | 26 | s.close();↵
|
| | | 27 | }↵
|
|
20 | s = openSession();↵ | | 28 | s = openSession();↵
|
21 | t = s.beginTransaction();↵ | | 29 | t = s.beginTransaction();↵
|
22 | c = (Contract) s.createCriteria(Contract.class).uniqueResult();↵ | | 30 | c = (Contract) s.createCriteria(Contract.class).uniqueResult();↵
|
23 | assertEquals( c.getCustomerName(), "gavin" );↵ | | 31 | assertEquals( c.getCustomerName(), "gavin" );↵
|
24 | assertEquals( c.getVariations().size(), 2 );↵ | | 32 | assertEquals( c.getVariations().size(), 2 );↵
|
25 | Iterator it = c.getVariations().iterator();↵ | | 33 | Iterator it = c.getVariations().iterator();↵
|
26 | cv1 = (ContractVariation) it.next();↵ | | 34 | cv1 = (ContractVariation) it.next();↵
|
27 | assertEquals( cv1.getText(), "expensive" );↵ | | 35 | assertEquals( cv1.getText(), "expensive" );↵
|
28 | cv2 = (ContractVariation) it.next();↵ | | 36 | cv2 = (ContractVariation) it.next();↵
|
29 | assertEquals( cv2.getText(), "more expensive" );↵ | | 37 | assertEquals( cv2.getText(), "more expensive" );↵
|
30 | s.delete(c);↵ | | 38 | s.delete(c);↵
|
31 | assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵ | | 39 | assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵
|
32 | assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵ | | 40 | assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );↵
|
33 | t.commit();↵ | | 41 | t.commit();↵
|
34 | s.close();↵ | | 42 | s.close();↵
|
35 | | | 43 |
|