From 0510ea8954b26faed8530f2d8cfffb39445db059 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Mon, 26 Sep 2016 15:22:21 -0400 Subject: [PATCH] HHH-11133 - Add discriminator-value attribute support for joined-subclass hbm mapping elements. (cherry picked from commit 2d03ba97479c0e984991f0477da1d67fad9a0fd4) --- .../hbm/JoinedSubclassEntitySourceImpl.java | 7 + .../xsd/mapping/legacy-mapping-4.0.xsd | 1 + .../discriminator/joined/ChildEntity.java | 31 ++++ .../joined/JoinedSubclassInheritance.hbm.xml | 34 ++++ .../joined/JoinedSubclassInheritanceTest.java | 50 +++++ .../discriminator/joined/ParentEntity.java | 39 ++++ .../DiscriminatorJoinedInheritanceTest.java | 174 ++++++++++++++++++ 7 files changed, 336 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ChildEntity.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritance.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ParentEntity.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/DiscriminatorJoinedInheritanceTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/JoinedSubclassEntitySourceImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/JoinedSubclassEntitySourceImpl.java index e8cdb181e3..1370c3f82b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/JoinedSubclassEntitySourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/JoinedSubclassEntitySourceImpl.java @@ -88,4 +88,11 @@ public class JoinedSubclassEntitySourceImpl extends SubclassEntitySourceImpl imp public List getPrimaryKeyColumnSources() { return primaryKeyJoinColumnSources; } + + @Override + public String getDiscriminatorMatchValue() { + return JaxbHbmJoinedSubclassEntityType.class.isInstance( jaxbEntityMapping() ) + ? ( (JaxbHbmJoinedSubclassEntityType) jaxbEntityMapping() ).getDiscriminatorValue() + : null; + } } diff --git a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd index bbe1a98cac..d199ff976e 100644 --- a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd @@ -497,6 +497,7 @@ + diff --git a/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ChildEntity.java b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ChildEntity.java new file mode 100644 index 0000000000..6581362e50 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ChildEntity.java @@ -0,0 +1,31 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.discriminator.joined; + +/** + * @author Chris Cranford + */ +public class ChildEntity extends ParentEntity { + private String name; + + ChildEntity() { + + } + + ChildEntity(Integer id, String name) { + super( id ); + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritance.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritance.hbm.xml new file mode 100644 index 0000000000..00601f8556 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritance.hbm.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java new file mode 100644 index 0000000000..c0b720212b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java @@ -0,0 +1,50 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.discriminator.joined; + +import org.hibernate.Session; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Chris Cranford + */ +@TestForIssue(jiraKey = "HHH-11133") +public class JoinedSubclassInheritanceTest extends BaseCoreFunctionalTestCase { + + @Override + protected String[] getMappings() { + return new String[] { "discriminator/joined/JoinedSubclassInheritance.hbm.xml" }; + } + + @Test + public void testConfiguredDiscriminatorValue() { + Session session = openSession(); + try { + ChildEntity ce = new ChildEntity( 1, "Child" ); + session.getTransaction().begin(); + session.save( ce ); + session.getTransaction().commit(); + session.clear(); + + ce = (ChildEntity) session.find( ChildEntity.class, 1 ); + assertEquals( "ce", ce.getType() ); + } + catch ( Exception e ) { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + finally { + session.close(); + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ParentEntity.java b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ParentEntity.java new file mode 100644 index 0000000000..60fe4dccd6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/ParentEntity.java @@ -0,0 +1,39 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.discriminator.joined; + +/** + * @author Chris Cranford + */ +public abstract class ParentEntity { + private Integer id; + private String type; + + ParentEntity() { + + } + + ParentEntity(Integer id) { + this.id = id; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/DiscriminatorJoinedInheritanceTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/DiscriminatorJoinedInheritanceTest.java new file mode 100644 index 0000000000..e0c75492e4 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/DiscriminatorJoinedInheritanceTest.java @@ -0,0 +1,174 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.envers.test.integration.inheritance.joined; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.OneToMany; + +import org.hibernate.envers.Audited; +import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; +import org.hibernate.envers.test.Priority; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Chris Cranford + */ +@TestForIssue(jiraKey = "HHH-11133") +public class DiscriminatorJoinedInheritanceTest extends BaseEnversJPAFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { ParentEntity.class, ChildEntity.class, ChildListHolder.class }; + } + + @Test + @Priority(10) + public void initData() { + EntityManager entityManager = getEntityManager(); + try { + ChildEntity childEntity = new ChildEntity( 1, "Child" ); + entityManager.getTransaction().begin(); + entityManager.persist( childEntity ); + entityManager.getTransaction().commit(); + + ChildListHolder holder = new ChildListHolder(); + holder.setId( 1 ); + holder.setChildren( Arrays.asList( childEntity ) ); + entityManager.getTransaction().begin(); + entityManager.persist( holder ); + entityManager.getTransaction().commit(); + + } + catch ( Exception e ) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + finally { + entityManager.close(); + } + } + + @Test + public void testRevisionCounts() { + assertEquals( Arrays.asList( 1 ), getAuditReader().getRevisions( ChildEntity.class, 1 ) ); + assertEquals( Arrays.asList( 2 ), getAuditReader().getRevisions( ChildListHolder.class, 1 ) ); + } + + @Test + public void testConfiguredDiscriminatorValue() { + ChildEntity entity = getAuditReader().find( ChildEntity.class, 1, 1 ); + assertEquals( "ce", entity.getType() ); + } + + @Test + public void testDiscriminatorValuesViaRelatedEntityQuery() { + ChildListHolder holder = getAuditReader().find( ChildListHolder.class, 1, 2 ); + assertEquals( 1, holder.getChildren().size() ); + assertEquals( "ce", holder.getChildren().get( 0 ).getType() ); + } + + @Entity(name = "ParentEntity") + @Audited + @Inheritance(strategy = InheritanceType.JOINED) + @DiscriminatorValue("pe") + @DiscriminatorColumn(name = "type", length = 255) + public static abstract class ParentEntity { + @Id + private Integer id; + + @Column(insertable = false, updatable = false) + private String type; + + ParentEntity() { + + } + + ParentEntity(Integer id) { + this.id = id; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getType() { + return type; + } + + private void setType(String type) { + this.type = type; + } + } + + @Entity(name = "ChildEntity") + @Audited + @DiscriminatorValue("ce") + public static class ChildEntity extends ParentEntity { + private String name; + + ChildEntity() { + + } + + ChildEntity(Integer id, String name) { + super( id ); + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "ChildListHolder") + @Audited + public static class ChildListHolder { + @Id + private Integer id; + @OneToMany + private List children; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + } +}