HHH-5319 Add more tests on @OneToMany @JoinTable unidirectional

In particular, test delete of the owning object wo the associated objects
and clean up properly after the test.

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19766 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-06-18 16:40:00 +00:00
parent 7e9ddb926a
commit e6862c0be0
2 changed files with 25 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@ -172,7 +173,28 @@ public class OneToManyTest extends TestCase {
assertNotNull( trainer );
assertNotNull( trainer.getTrainedMonkeys() );
assertEquals( 2, trainer.getTrainedMonkeys().size() );
tx.rollback();
//test suppression of trainer wo monkey
final Set<Monkey> monkeySet = new HashSet( trainer.getTrainedMonkeys() );
s.delete( trainer );
s.flush();
tx.commit();
s.clear();
tx = s.beginTransaction();
for ( Monkey m : monkeySet ) {
final Object managedMonkey = s.get( Monkey.class, m.getId() );
assertNotNull( "No trainers but monkeys should still be here", managedMonkey );
}
//clean up
for ( Monkey m : monkeySet ) {
final Object managedMonkey = s.get( Monkey.class, m.getId() );
s.delete(managedMonkey);
}
s.flush();
tx.commit();
s.close();
}

View File

@ -53,7 +53,8 @@ public class Trainer {
@OneToMany
@JoinTable(
name = "TrainedMonkeys",
joinColumns = {@JoinColumn(name = "trainer_id")},
//columns are optional, here we explicit them
joinColumns = @JoinColumn(name = "trainer_id"),
inverseJoinColumns = @JoinColumn(name = "monkey_id")
)
@ForeignKey(name = "TM_TRA_FK", inverseName = "TM_MON_FK")