From 9f85525e456cbf4c7ca3d175e4c5befef06a1137 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Tue, 26 Jan 2010 13:26:07 +0000 Subject: [PATCH] HHH-4529 Add support for parent's id as IdClass and derived entity reusing the same PK class than the parent git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18627 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/cfg/AnnotationBinder.java | 4 +- ...dentityIdClassParentSameIdTypeDepTest.java | 45 +++++++++++++++++++ .../e5/b/MedicalHistory.java | 27 +++++++++++ .../derivedidentities/e5/b/Person.java | 17 +++++++ .../derivedidentities/e5/b/PersonId.java | 11 +++++ 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/DerivedIdentityIdClassParentSameIdTypeDepTest.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/MedicalHistory.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/Person.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/PersonId.java diff --git a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java index c0d3168bd8..6ad7eb0edd 100644 --- a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -2000,9 +2000,9 @@ public final class + " must not have @Id properties when used as an @EmbeddedId: " + BinderHelper.getPath( propertyHolder, inferredData ) ); } - if ( comp.getPropertySpan() == 0 ) { + if ( referencedEntityName == null && comp.getPropertySpan() == 0 ) { throw new AnnotationException( comp.getComponentClassName() - + " has no persistent id property" + + " has no persistent id property: " + BinderHelper.getPath( propertyHolder, inferredData ) ); } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/DerivedIdentityIdClassParentSameIdTypeDepTest.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/DerivedIdentityIdClassParentSameIdTypeDepTest.java new file mode 100644 index 0000000000..a5da7d183f --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/DerivedIdentityIdClassParentSameIdTypeDepTest.java @@ -0,0 +1,45 @@ +package org.hibernate.test.annotations.derivedidentities.e5.b; + +import java.util.Date; + +import org.hibernate.Session; +import org.hibernate.test.annotations.TestCase; +import org.hibernate.test.util.SchemaUtil; + +/** + * @author Emmanuel Bernard + */ +public class DerivedIdentityIdClassParentSameIdTypeDepTest extends TestCase { + + public void testOneToOneExplicitJoinColumn() throws Exception { + assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", getCfg() ) ); + assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", getCfg() ) ); + assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", getCfg() ) ); + Person e = new Person(); + e.firstName = "Emmanuel"; + e.lastName = "Bernard"; + Session s = openSession( ); + s.getTransaction().begin(); + s.persist( e ); + MedicalHistory d = new MedicalHistory(); + d.id = new PersonId(); + d.id.firstName = "Emmanuel"; //FIXME not needed when foreign is enabled + d.id.lastName = "Bernard"; //FIXME not needed when foreign is enabled + d.patient = e; + s.persist( d ); + s.flush(); + s.clear(); + d = (MedicalHistory) s.get( MedicalHistory.class, d.id ); + assertEquals( d.id.firstName, d.patient.firstName ); + s.getTransaction().rollback(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + MedicalHistory.class, + Person.class + }; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/MedicalHistory.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/MedicalHistory.java new file mode 100644 index 0000000000..aee86e6555 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/MedicalHistory.java @@ -0,0 +1,27 @@ +package org.hibernate.test.annotations.derivedidentities.e5.b; + +import java.util.Date; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class MedicalHistory { + //all attributes map to relationship: AttributeOverride not allowed + @EmbeddedId + PersonId id; + + @MapsId + @JoinColumns({ + @JoinColumn(name = "FK1", referencedColumnName = "firstName"), + @JoinColumn(name = "FK2", referencedColumnName = "lastName") + }) + @OneToOne + Person patient; +} \ No newline at end of file diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/Person.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/Person.java new file mode 100644 index 0000000000..141d77ddcb --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/Person.java @@ -0,0 +1,17 @@ +package org.hibernate.test.annotations.derivedidentities.e5.b; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; + +/** + * @author Emmanuel Bernard + */ +@Entity +@IdClass(PersonId.class) +public class Person { + @Id + String firstName; + @Id + String lastName; +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/PersonId.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/PersonId.java new file mode 100644 index 0000000000..8c10224d43 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/b/PersonId.java @@ -0,0 +1,11 @@ +package org.hibernate.test.annotations.derivedidentities.e5.b; + +import java.io.Serializable; + +/** + * @author Emmanuel Bernard + */ +public class PersonId implements Serializable { + String firstName; + String lastName; +}