HHH-11133 - Add discriminator-value attribute support for joined-subclass hbm mapping elements.

This commit is contained in:
Chris Cranford 2016-09-26 15:22:21 -04:00 committed by Steve Ebersole
parent de3153a8e1
commit 2d03ba9747
7 changed files with 336 additions and 0 deletions

View File

@ -88,4 +88,11 @@ public class JoinedSubclassEntitySourceImpl extends SubclassEntitySourceImpl imp
public List<ColumnSource> getPrimaryKeyColumnSources() { public List<ColumnSource> getPrimaryKeyColumnSources() {
return primaryKeyJoinColumnSources; return primaryKeyJoinColumnSources;
} }
@Override
public String getDiscriminatorMatchValue() {
return JaxbHbmJoinedSubclassEntityType.class.isInstance( jaxbEntityMapping() )
? ( (JaxbHbmJoinedSubclassEntityType) jaxbEntityMapping() ).getDiscriminatorValue()
: null;
}
} }

View File

@ -497,6 +497,7 @@
</xs:sequence> </xs:sequence>
<xs:attributeGroup ref="table-information-group"/> <xs:attributeGroup ref="table-information-group"/>
<xs:attribute name="check" type="xs:string"/> <xs:attribute name="check" type="xs:string"/>
<xs:attribute name="discriminator-value" type="xs:string" />
</xs:extension> </xs:extension>
</xs:complexContent> </xs:complexContent>
</xs:complexType> </xs:complexType>

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -0,0 +1,34 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.test.discriminator.joined"
default-access="field">
<class name="org.hibernate.test.discriminator.joined.ParentEntity" discriminator-value="pe">
<id name="id" type="integer">
<column name="id"/>
</id>
<discriminator type="string">
<column name="type" length="255" />
</discriminator>
<property name="type" insert="false" update="false" type="string"/>
</class>
<joined-subclass name="org.hibernate.test.discriminator.joined.ChildEntity" discriminator-value="ce" extends="org.hibernate.test.discriminator.joined.ParentEntity">
<key>
<column name="id" />
</key>
<property name="name" type="string"/>
</joined-subclass>
</hibernate-mapping>

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<ChildEntity> children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<ChildEntity> getChildren() {
return children;
}
public void setChildren(List<ChildEntity> children) {
this.children = children;
}
}
}