From 0600527a6b250cbabb20dc1445014fe636591d77 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Fri, 14 Jan 2011 15:52:43 +0100 Subject: [PATCH] HHH-5280 Add test failing on multiple owners --- .../annotations/engine/collection/Father.java | 35 +++++++++++++ .../annotations/engine/collection/Mother.java | 31 +++++++++++ .../annotations/engine/collection/Son.java | 31 +++++++++++ ...UnidirCollectionWithMultipleOwnerTest.java | 51 +++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Father.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Mother.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Son.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/UnidirCollectionWithMultipleOwnerTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Father.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Father.java new file mode 100644 index 0000000000..8e231d652a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Father.java @@ -0,0 +1,35 @@ +package org.hibernate.test.annotations.engine.collection; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OrderColumn; +import javax.persistence.Table; + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +/** + * @author Emmanuel Bernard + */ +@Entity +@Table(name = "co_father") +public class Father { + @Id + @GeneratedValue + public Integer getId() { return id; } + public void setId(Integer id) { this.id = id; } + private Integer id; + + @OneToMany + @OrderColumn(name = "son_arriv") + @JoinColumn(name = "father_id", nullable = false) + @Cascade({ CascadeType.SAVE_UPDATE }) + public List getOrderedSons() { return orderedSons; } + public void setOrderedSons(List orderedSons) { this.orderedSons = orderedSons; } + private List orderedSons = new ArrayList( ); +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Mother.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Mother.java new file mode 100644 index 0000000000..156fd797bf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Mother.java @@ -0,0 +1,31 @@ +package org.hibernate.test.annotations.engine.collection; + +import java.util.HashSet; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +/** + * @author Emmanuel Bernard + */ +@Entity +@Table(name = "co_mother") +public class Mother { + @Id + @GeneratedValue + public Integer getId() { return id; } + public void setId(Integer id) { this.id = id; } + private Integer id; + + @OneToMany(mappedBy = "mother") + @Cascade({ CascadeType.SAVE_UPDATE }) + public Set getSons() { return sons; } + public void setSons(Set sons) { this.sons = sons; } + private Set sons = new HashSet(); +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Son.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Son.java new file mode 100644 index 0000000000..59e6201fe0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/Son.java @@ -0,0 +1,31 @@ +package org.hibernate.test.annotations.engine.collection; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +/** + * @author Emmanuel Bernard + */ +@Entity +@Table(name="co_son") +public class Son { + @Id + @GeneratedValue + public Integer getId() { return id; } + public void setId(Integer id) { this.id = id; } + private Integer id; + + @ManyToOne(optional = false) @JoinColumn(name = "father_id", insertable = false, updatable = false, nullable = false) + public Father getFather() { return father; } + public void setFather(Father father) { this.father = father; } + private Father father; + + @ManyToOne + public Mother getMother() { return mother; } + public void setMother(Mother mother) { this.mother = mother; } + private Mother mother; +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/UnidirCollectionWithMultipleOwnerTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/UnidirCollectionWithMultipleOwnerTest.java new file mode 100644 index 0000000000..16d59c9a5b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/engine/collection/UnidirCollectionWithMultipleOwnerTest.java @@ -0,0 +1,51 @@ +package org.hibernate.test.annotations.engine.collection; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.test.annotations.TestCase; + +/** + * @author Emmanuel Bernard + */ +public class UnidirCollectionWithMultipleOwnerTest extends TestCase { + + public void testUnidirCollectionWithMultipleOwner() throws Exception { + Session s = openSession(); + Transaction tx; + tx = s.beginTransaction(); + Father father = new Father(); + Mother mother = new Mother(); + s.save( father ); + //s.save( mother ); + Son son = new Son(); + father.getOrderedSons().add( son ); + son.setFather( father ); + mother.getSons().add( son ); + son.setMother( mother ); + s.save( mother ); + s.save( father ); + tx.commit(); + + s.clear(); + + tx = s.beginTransaction(); + son = (Son) s.get( Son.class, son.getId() ); + s.delete( son ); + s.flush(); + father = (Father) s.get( Father.class, father.getId() ); + mother = (Mother) s.get( Mother.class, mother.getId() ); + s.delete( father ); + s.delete( mother ); + tx.commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Father.class, + Mother.class, + Son.class + }; + } +}