diff --git a/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java b/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java index 504946a7e1..f17a83b110 100644 --- a/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java +++ b/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java @@ -99,13 +99,13 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i ); } + final Class idClass = persister.getIdentifierType().getReturnedClass(); if ( persister.getIdentifierType().isComponentType() && EntityMode.DOM4J == event.getSession().getEntityMode() ) { // skip this check for composite-ids relating to dom4j entity-mode; // alternatively, we could add a check to make sure the incoming id value is // an instance of Element... } else { - Class idClass = persister.getIdentifierType().getReturnedClass(); if ( idClass != null && ! idClass.isInstance( event.getEntityId() ) ) { // we may have the kooky jpa requirement of allowing find-by-id where // "id" is the "simple pk value" of a dependent objects parent. This @@ -542,7 +542,8 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i } } if ( options.isAllowNulls() ) { - EntityPersister persister = event.getSession().getFactory().getEntityPersister( event.getEntityClassName() ); +// EntityPersister persister = event.getSession().getFactory().getEntityPersister( event.getEntityClassName() ); + EntityPersister persister = event.getSession().getFactory().getEntityPersister( keyToLoad.getEntityName() ); if ( ! persister.isInstance( old, event.getSession().getEntityMode() ) ) { return INCONSISTENT_RTN_CLASS_MARKER; } diff --git a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java index 2a6a691d9b..088d4ff042 100644 --- a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java +++ b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java @@ -8,30 +8,64 @@ import org.hibernate.test.util.SchemaUtil; * @author Emmanuel Bernard */ public class DerivedIdentityIdClassParentSameIdTypeIdClassDepTest extends TestCase { - + private static final String FIRST_NAME = "Emmanuel"; + private static final String LAST_NAME = "Bernard"; + 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( ); + + Session s = openSession(); s.getTransaction().begin(); + Person e = new Person( FIRST_NAME, LAST_NAME ); s.persist( e ); - MedicalHistory d = new MedicalHistory(); - d.patient = e; + MedicalHistory d = new MedicalHistory( e ); s.persist( d ); s.flush(); - s.clear(); - PersonId pId = new PersonId(); - pId.firstName = e.firstName; - pId.lastName = e.lastName; - d = (MedicalHistory) s.get( MedicalHistory.class, pId); - assertEquals( pId.firstName, d.patient.firstName ); - s.delete( d ); - s.delete( d.patient ); - s.getTransaction().rollback(); + s.refresh( d ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + PersonId pId = new PersonId( FIRST_NAME, LAST_NAME ); + MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId ); + Person p2 = (Person) s.get( Person.class, pId ); + assertEquals( pId.firstName, d2.patient.firstName ); + assertEquals( pId.firstName, p2.firstName ); + s.delete( d2 ); + s.delete( p2 ); + s.getTransaction().commit(); + s.close(); + } + + public void testTckLikeBehavior() throws Exception { + assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", getCfg() ) ); + assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", getCfg() ) ); + assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", getCfg() ) ); + + Session s = openSession(); + s.getTransaction().begin(); + Person e = new Person( FIRST_NAME, LAST_NAME ); + s.persist( e ); + MedicalHistory d = new MedicalHistory( e ); + s.persist( d ); + s.flush(); + s.refresh( d ); + s.getTransaction().commit(); + + // NOTE THAT WE LEAVE THE SESSION OPEN! + + s.getTransaction().begin(); + PersonId pId = new PersonId( FIRST_NAME, LAST_NAME ); + MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId ); + Person p2 = (Person) s.get( Person.class, pId ); + assertEquals( pId.firstName, d2.patient.firstName ); + assertEquals( pId.firstName, p2.firstName ); + s.delete( d2 ); + s.delete( p2 ); + s.getTransaction().commit(); s.close(); } diff --git a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java index 1e11da9dea..824d99a6aa 100644 --- a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java +++ b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java @@ -21,4 +21,11 @@ public class MedicalHistory implements Serializable { }) @OneToOne Person patient; + + public MedicalHistory() { + } + + public MedicalHistory(Person patient) { + this.patient = patient; + } } diff --git a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java index 4e9d1794af..c807c134f0 100644 --- a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java +++ b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java @@ -13,4 +13,12 @@ import javax.persistence.IdClass; public class Person { @Id String firstName; @Id String lastName; + + public Person() { + } + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } } diff --git a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java index fdcf5e6d3a..6fbf4e14f0 100644 --- a/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java +++ b/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java @@ -8,4 +8,12 @@ import java.io.Serializable; public class PersonId implements Serializable { String firstName; String lastName; + + public PersonId() { + } + + public PersonId(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } }