diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java index 66757be0..9368c78f 100644 --- a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java @@ -17,9 +17,14 @@ import org.hibernate.Session; import org.hibernate.Transaction; +import org.hibernate.bugs.entities.EntityChildTwoSameTable; +import org.hibernate.bugs.entities.EntityParent; +import org.hibernate.bugs.entities.EntityChildOne; +import org.hibernate.bugs.entities.EntityRelation; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Assert; import org.junit.Test; /** @@ -37,8 +42,12 @@ public class ORMUnitTestCase extends BaseCoreFunctionalTestCase { @Override protected Class[] getAnnotatedClasses() { return new Class[] { -// Foo.class, -// Bar.class + EntityChildTwoSameTable.class, + EntityParent.class, + EntityChildOne.class, + EntityRelation.class + // Foo.class, + // Bar.class }; } @@ -46,8 +55,8 @@ protected Class[] getAnnotatedClasses() { @Override protected String[] getMappings() { return new String[] { -// "Foo.hbm.xml", -// "Bar.hbm.xml" + // "Foo.hbm.xml", + // "Bar.hbm.xml" }; } // If those mappings reside somewhere other than resources/org/hibernate/test, change this. @@ -68,12 +77,35 @@ protected void configure(Configuration configuration) { // Add your tests, using standard JUnit. @Test - public void hhh123Test() throws Exception { + public void hhh19457Test() throws Exception { // BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session. Session s = openSession(); Transaction tx = s.beginTransaction(); + + EntityRelation entityRelation = new EntityRelation(); + entityRelation.setId("idRelation1"); + s.save(entityRelation); + + EntityChildOne entityChildOne = new EntityChildOne(); + entityChildOne.setId("idChild1"); + entityChildOne.setName("nameChild1"); + entityChildOne.setIdRelation("idRelation1"); + s.save(entityChildOne); + + EntityChildTwoSameTable entityChildTwo = new EntityChildTwoSameTable(); + entityChildTwo.setId("idChild2"); + entityChildTwo.setChildTwoName("nameChild2"); + entityChildTwo.setIdRelation("idRelation1"); + s.save(entityChildTwo); // Do stuff... tx.commit(); s.close(); + + s = openSession(); + tx = s.beginTransaction(); + EntityRelation relation = s.get(EntityRelation.class, "idRelation1"); + Assert.assertEquals("must have 2 parents", 2, relation.getParents().size()); + tx.commit(); + s.close(); } } diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityChildOne.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityChildOne.java new file mode 100644 index 00000000..0c415391 --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityChildOne.java @@ -0,0 +1,23 @@ +package org.hibernate.bugs.entities; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.Table; + +@Entity +@Table(name = "CHILD") +@DiscriminatorValue("15") +public class EntityChildOne extends EntityParent { + + @Column(name = "name") + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityChildTwoSameTable.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityChildTwoSameTable.java new file mode 100644 index 00000000..f6d7dfcc --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityChildTwoSameTable.java @@ -0,0 +1,23 @@ +package org.hibernate.bugs.entities; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.Table; + +@Entity +@Table(name = "CHILD") +@DiscriminatorValue("24") +public class EntityChildTwoSameTable extends EntityParent { + + @Column(name = "child_two_name") + private String childTwoName; + + public String getChildTwoName() { + return childTwoName; + } + + public void setChildTwoName(String childTwoName) { + this.childTwoName = childTwoName; + } +} diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityParent.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityParent.java new file mode 100644 index 00000000..274b4b05 --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityParent.java @@ -0,0 +1,58 @@ + +package org.hibernate.bugs.entities; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity(name = "PARENT") +@Table(name = "PARENT") +@Inheritance(strategy = InheritanceType.JOINED) +@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "DISCRIMINATOR") +public abstract class EntityParent implements Serializable { + + @Id + @Column(name = "id") + private String id; + + @Column(name = "id_relation") + private String idRelation; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "id_relation", referencedColumnName = "id", insertable = false, updatable = false) + private EntityRelation relation; + + public EntityRelation getRelation() { + return relation; + } + + public void setRelation(EntityRelation requisition) { + this.relation = requisition; + } + + public String getIdRelation() { + return idRelation; + } + + public void setIdRelation(String idRelation) { + this.idRelation = idRelation; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +} diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityRelation.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityRelation.java new file mode 100644 index 00000000..9aadcdc4 --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/entities/EntityRelation.java @@ -0,0 +1,38 @@ + +package org.hibernate.bugs.entities; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +@Entity(name = "RELATION") +@Table(name = "RELATION") +public class EntityRelation { + @Id + @Column(name = "id") + private String id; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "relation") + private List parents; + + public void setId(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public List getParents() { + return parents; + } + + public void setParents(List demandes) { + this.parents = demandes; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java index eefb7df4..fce27596 100644 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java @@ -15,6 +15,10 @@ */ package org.hibernate.bugs; +import org.hibernate.bugs.entities.EntityChildOne; +import org.hibernate.bugs.entities.EntityChildTwoSameTable; +import org.hibernate.bugs.entities.EntityParent; +import org.hibernate.bugs.entities.EntityRelation; import org.hibernate.cfg.AvailableSettings; import org.hibernate.testing.orm.junit.DomainModel; @@ -22,6 +26,7 @@ import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; +import org.junit.Assert; import org.junit.jupiter.api.Test; /** @@ -34,37 +39,61 @@ * submit it as a PR! */ @DomainModel( - annotatedClasses = { - // Add your entities here. - // Foo.class, - // Bar.class - }, - // If you use *.hbm.xml mappings, instead of annotations, add the mappings here. - xmlMappings = { - // "org/hibernate/test/Foo.hbm.xml", - // "org/hibernate/test/Bar.hbm.xml" - } + annotatedClasses = { + // Add your entities here. + EntityChildTwoSameTable.class, + EntityParent.class, + EntityChildOne.class, + EntityRelation.class + // Bar.class + }, + // If you use *.hbm.xml mappings, instead of annotations, add the mappings here. + xmlMappings = { + // "org/hibernate/test/Foo.hbm.xml", + // "org/hibernate/test/Bar.hbm.xml" + } ) @ServiceRegistry( - // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. - settings = { - // For your own convenience to see generated queries: - @Setting(name = AvailableSettings.SHOW_SQL, value = "true"), - @Setting(name = AvailableSettings.FORMAT_SQL, value = "true"), - // @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. + settings = { + // For your own convenience to see generated queries: + @Setting(name = AvailableSettings.SHOW_SQL, value = "true"), + @Setting(name = AvailableSettings.FORMAT_SQL, value = "true"), + // @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), - // Add your own settings that are a part of your quarkus configuration: - // @Setting( name = AvailableSettings.SOME_CONFIGURATION_PROPERTY, value = "SOME_VALUE" ), - } + // Add your own settings that are a part of your quarkus configuration: + // @Setting( name = AvailableSettings.SOME_CONFIGURATION_PROPERTY, value = "SOME_VALUE" ), + } ) @SessionFactory class ORMUnitTestCase { // Add your tests, using standard JUnit 5. @Test - void hhh123Test(SessionFactoryScope scope) throws Exception { - scope.inTransaction( session -> { + void hhh19457Test(SessionFactoryScope scope) throws Exception { + scope.inTransaction( s -> { // Do stuff... + EntityRelation entityRelation = new EntityRelation(); + entityRelation.setId("idRelation1"); + s.save(entityRelation); + + EntityChildOne entityChildOne = new EntityChildOne(); + entityChildOne.setId("idChild1"); + entityChildOne.setName("nameChild1"); + entityChildOne.setIdRelation("idRelation1"); + s.save(entityChildOne); + + EntityChildTwoSameTable entityChildTwo = new EntityChildTwoSameTable(); + entityChildTwo.setId("idChild2"); + entityChildTwo.setChildTwoName("nameChild2"); + entityChildTwo.setIdRelation("idRelation1"); + s.save(entityChildTwo); } ); + + scope.inSession(s -> { + EntityRelation relation = s.get(EntityRelation.class, "idRelation1"); + // If you disable the -ea flag, the assertion will fail. + Assert.assertEquals("must have 2 parents", 2, relation.getParents().size()); + }); } } diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityChildOne.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityChildOne.java new file mode 100644 index 00000000..83bb81e0 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityChildOne.java @@ -0,0 +1,24 @@ + +package org.hibernate.bugs.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.DiscriminatorValue; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +@Entity +@Table(name = "CHILD") +@DiscriminatorValue("15") +public class EntityChildOne extends EntityParent { + + @Column(name = "name") + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityChildTwoSameTable.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityChildTwoSameTable.java new file mode 100644 index 00000000..5f99c333 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityChildTwoSameTable.java @@ -0,0 +1,24 @@ + +package org.hibernate.bugs.entities; + +import jakarta.persistence.Column; +import jakarta.persistence.DiscriminatorValue; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +@Entity +@Table(name = "CHILD") +@DiscriminatorValue("24") +public class EntityChildTwoSameTable extends EntityParent { + + @Column(name = "child_two_name") + private String childTwoName; + + public String getChildTwoName() { + return childTwoName; + } + + public void setChildTwoName(String childTwoName) { + this.childTwoName = childTwoName; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityParent.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityParent.java new file mode 100644 index 00000000..c55f9242 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityParent.java @@ -0,0 +1,58 @@ + +package org.hibernate.bugs.entities; + +import java.io.Serializable; + +import jakarta.persistence.Column; +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.DiscriminatorType; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; + +@Entity(name = "PARENT") +@Table(name = "PARENT") +@Inheritance(strategy = InheritanceType.JOINED) +@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "CODTYPDEM") +public abstract class EntityParent implements Serializable { + + @Id + @Column(name = "id") + private String id; + + @Column(name = "id_relation") + private String idRelation; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "id_relation", referencedColumnName = "id", insertable = false, updatable = false) + private EntityRelation relation; + + public EntityRelation getRelation() { + return relation; + } + + public void setRelation(EntityRelation requisition) { + this.relation = requisition; + } + + public String getIdRelation() { + return idRelation; + } + + public void setIdRelation(String idRelation) { + this.idRelation = idRelation; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityRelation.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityRelation.java new file mode 100644 index 00000000..1b38c631 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/entities/EntityRelation.java @@ -0,0 +1,38 @@ + +package org.hibernate.bugs.entities; + +import java.util.List; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +@Entity(name = "RELATION") +@Table(name = "RELATION") +public class EntityRelation { + @Id + @Column(name = "id") + private String id; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "relation") + private List parents; + + public void setId(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public List getParents() { + return parents; + } + + public void setParents(List demandes) { + this.parents = demandes; + } +}