HHH-4529 Add support for parent's id as EmbeddedId and derived entity reusing the same PK class than the parent

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18628 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-26 13:30:32 +00:00
parent 9f85525e45
commit 41ad6c01fd
5 changed files with 97 additions and 1 deletions

View File

@ -38,7 +38,7 @@ public class DerivedIdentityIdClassParentSameIdTypeDepTest extends TestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
MedicalHistory.class,
MedicalHistory.class,
Person.class
};
}

View File

@ -0,0 +1,44 @@
package org.hibernate.test.annotations.derivedidentities.e6.b;
import org.hibernate.Session;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.test.util.SchemaUtil;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityEmbeddedIdParentSameIdTypeDepTest 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.id = new PersonId();
e.id.firstName = "Emmanuel";
e.id.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.id.firstName );
s.getTransaction().rollback();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
MedicalHistory.class,
Person.class
};
}
}

View File

@ -0,0 +1,26 @@
package org.hibernate.test.annotations.derivedidentities.e6.b;
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;
}

View File

@ -0,0 +1,13 @@
package org.hibernate.test.annotations.derivedidentities.e6.b;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Person {
@EmbeddedId
PersonId id;
}

View File

@ -0,0 +1,13 @@
package org.hibernate.test.annotations.derivedidentities.e6.b;
import java.io.Serializable;
import javax.persistence.Embeddable;
/**
* @author Emmanuel Bernard
*/
@Embeddable
public class PersonId implements Serializable {
String firstName;
String lastName;
}