1
package org.hibernate.test.jpa.cascade;
3
import org.hibernate.test.jpa.AbstractJPATest;
4
import org.hibernate.Session;
5
import org.hibernate.TransientObjectException;
6
import org.hibernate.EntityMode;
7
import org.hibernate.HibernateException;
8
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
9
import org.hibernate.engine.Status;
10
import org.hibernate.persister.entity.EntityPersister;
11
import org.hibernate.event.FlushEntityEventListener;
12
import org.hibernate.event.FlushEntityEvent;
13
import org.hibernate.cfg.Configuration;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
import junit.framework.Test;
19
* According to the JPA spec, persist()ing an entity should throw an exception
20
* when said entity contains a reference to a transient entity through a mapped
21
* association where that association is not marked for cascading the persist
24
* This test-case tests that requirement in the various association style
25
* scenarios such as many-to-one, one-to-one, many-to-one (property-ref),
26
* one-to-one (property-ref). Additionally, it performs each of these tests
27
* in both generated and assigned identifier usages...
29
* @author Steve Ebersole
31
public class CascadeTest extends AbstractJPATest {
33
public static final Logger log = LoggerFactory.getLogger( CascadeTest.class );
35
public CascadeTest(String name) {
39
public String[] getMappings() {
40
return new String[] { "jpa/cascade/ParentChild.hbm.xml" };
43
public static Test suite() {
44
return new FunctionalTestClassTestSuite( CascadeTest.class );
47
public void testManyToOneGeneratedIdsOnSave() {
48
// NOTES: Child defines a many-to-one back to its Parent. This
49
// association does not define persist cascading (which is natural;
50
// a child should not be able to create its parent).
52
Session s = openSession();
54
Parent p = new Parent( "parent" );
55
Child c = new Child( "child" );
59
s.getTransaction().commit();
60
fail( "expecting TransientObjectException on flush" );
62
catch( TransientObjectException e ) {
64
log.trace( "handled expected exception", e );
65
s.getTransaction().rollback();
76
public void testManyToOneGeneratedIds() {
77
// NOTES: Child defines a many-to-one back to its Parent. This
78
// association does not define persist cascading (which is natural;
79
// a child should not be able to create its parent).
81
Session s = openSession();
83
Parent p = new Parent( "parent" );
84
Child c = new Child( "child" );
88
s.getTransaction().commit();
89
fail( "expecting TransientObjectException on flush" );
91
catch( TransientObjectException e ) {
93
log.trace( "handled expected exception", e );
94
s.getTransaction().rollback();
105
public void testManyToOneAssignedIds() {
106
// NOTES: Child defines a many-to-one back to its Parent. This
107
// association does not define persist cascading (which is natural;
108
// a child should not be able to create its parent).
110
Session s = openSession();
111
s.beginTransaction();
112
ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
113
ChildAssigned c = new ChildAssigned( new Long( 2 ), "child" );
117
s.getTransaction().commit();
118
fail( "expecting TransientObjectException on flush" );
120
catch( TransientObjectException e ) {
122
log.trace( "handled expected exception", e );
123
s.getTransaction().rollback();
134
public void testOneToOneGeneratedIds() {
136
Session s = openSession();
137
s.beginTransaction();
138
Parent p = new Parent( "parent" );
139
ParentInfo info = new ParentInfo( "xyz" );
144
s.getTransaction().commit();
145
fail( "expecting TransientObjectException on flush" );
147
catch( TransientObjectException e ) {
149
log.trace( "handled expected exception", e );
150
s.getTransaction().rollback();
161
public void testOneToOneAssignedIds() {
163
Session s = openSession();
164
s.beginTransaction();
165
ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
166
ParentInfoAssigned info = new ParentInfoAssigned( "something secret" );
171
s.getTransaction().commit();
172
fail( "expecting TransientObjectException on flush" );
174
catch( TransientObjectException e ) {
176
log.trace( "handled expected exception", e );
177
s.getTransaction().rollback();
188
public void testManyToOnePropertyRefGeneratedIds() {
190
Session s = openSession();
191
s.beginTransaction();
192
Parent p = new Parent( "parent" );
193
Other other = new Other();
197
s.getTransaction().commit();
198
fail( "expecting TransientObjectException on flush" );
200
catch( TransientObjectException e ) {
202
log.trace( "handled expected exception", e );
203
s.getTransaction().rollback();
214
public void testManyToOnePropertyRefAssignedIds() {
216
Session s = openSession();
217
s.beginTransaction();
218
ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
219
OtherAssigned other = new OtherAssigned( new Long( 2 ) );
223
s.getTransaction().commit();
224
fail( "expecting TransientObjectException on flush" );
226
catch( TransientObjectException e ) {
228
log.trace( "handled expected exception", e );
229
s.getTransaction().rollback();
240
public void testOneToOnePropertyRefGeneratedIds() {
242
Session s = openSession();
243
s.beginTransaction();
244
Child c2 = new Child( "c2" );
245
ChildInfo info = new ChildInfo( "blah blah blah" );
250
s.getTransaction().commit();
251
fail( "expecting TransientObjectException on flush" );
253
catch( TransientObjectException e ) {
255
log.trace( "handled expected exception : " + e );
256
s.getTransaction().rollback();
267
public void testOneToOnePropertyRefAssignedIds() {
269
Session s = openSession();
270
s.beginTransaction();
271
ChildAssigned c2 = new ChildAssigned( new Long( 3 ), "c3" );
272
ChildInfoAssigned info = new ChildInfoAssigned( new Long( 4 ), "blah blah blah" );
277
s.getTransaction().commit();
278
fail( "expecting TransientObjectException on flush" );
280
catch( TransientObjectException e ) {
282
log.trace( "handled expected exception : " + e );
283
s.getTransaction().rollback();
295
private void cleanupData() {
299
s.beginTransaction();
300
s.createQuery( "delete ChildInfoAssigned" ).executeUpdate();
301
s.createQuery( "delete ChildAssigned" ).executeUpdate();
302
s.createQuery( "delete ParentAssigned" ).executeUpdate();
303
s.createQuery( "delete ChildInfoAssigned" ).executeUpdate();
304
s.createQuery( "delete ChildAssigned" ).executeUpdate();
305
s.createQuery( "delete ParentAssigned" ).executeUpdate();
306
s.getTransaction().commit();
308
catch( Throwable t ) {
309
log.warn( "unable to cleanup test data [" + fullTestName() + "] : " + t );
316
catch( Throwable ignore ) {