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