HHH-5629 - Inconsistent consistency checks for entities already found in session

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20780 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-10-06 17:39:33 +00:00
parent 6c6e6ab193
commit 47eace5163
5 changed files with 76 additions and 18 deletions

View File

@ -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() ) { if ( persister.getIdentifierType().isComponentType() && EntityMode.DOM4J == event.getSession().getEntityMode() ) {
// skip this check for composite-ids relating to dom4j entity-mode; // 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 // alternatively, we could add a check to make sure the incoming id value is
// an instance of Element... // an instance of Element...
} }
else { else {
Class idClass = persister.getIdentifierType().getReturnedClass();
if ( idClass != null && ! idClass.isInstance( event.getEntityId() ) ) { if ( idClass != null && ! idClass.isInstance( event.getEntityId() ) ) {
// we may have the kooky jpa requirement of allowing find-by-id where // 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 // "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() ) { 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() ) ) { if ( ! persister.isInstance( old, event.getSession().getEntityMode() ) ) {
return INCONSISTENT_RTN_CLASS_MARKER; return INCONSISTENT_RTN_CLASS_MARKER;
} }

View File

@ -8,30 +8,64 @@ import org.hibernate.test.util.SchemaUtil;
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public class DerivedIdentityIdClassParentSameIdTypeIdClassDepTest extends TestCase { public class DerivedIdentityIdClassParentSameIdTypeIdClassDepTest extends TestCase {
private static final String FIRST_NAME = "Emmanuel";
private static final String LAST_NAME = "Bernard";
public void testOneToOneExplicitJoinColumn() throws Exception { public void testOneToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", getCfg() ) ); assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", getCfg() ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", getCfg() ) ); assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", getCfg() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", getCfg() ) ); assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", getCfg() ) );
Person e = new Person();
e.firstName = "Emmanuel"; Session s = openSession();
e.lastName = "Bernard";
Session s = openSession( );
s.getTransaction().begin(); s.getTransaction().begin();
Person e = new Person( FIRST_NAME, LAST_NAME );
s.persist( e ); s.persist( e );
MedicalHistory d = new MedicalHistory(); MedicalHistory d = new MedicalHistory( e );
d.patient = e;
s.persist( d ); s.persist( d );
s.flush(); s.flush();
s.clear(); s.refresh( d );
PersonId pId = new PersonId(); s.getTransaction().commit();
pId.firstName = e.firstName; s.close();
pId.lastName = e.lastName;
d = (MedicalHistory) s.get( MedicalHistory.class, pId); s = openSession();
assertEquals( pId.firstName, d.patient.firstName ); s.getTransaction().begin();
s.delete( d ); PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
s.delete( d.patient ); MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId );
s.getTransaction().rollback(); 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(); s.close();
} }

View File

@ -21,4 +21,11 @@ public class MedicalHistory implements Serializable {
}) })
@OneToOne @OneToOne
Person patient; Person patient;
public MedicalHistory() {
}
public MedicalHistory(Person patient) {
this.patient = patient;
}
} }

View File

@ -13,4 +13,12 @@ import javax.persistence.IdClass;
public class Person { public class Person {
@Id String firstName; @Id String firstName;
@Id String lastName; @Id String lastName;
public Person() {
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
} }

View File

@ -8,4 +8,12 @@ import java.io.Serializable;
public class PersonId implements Serializable { public class PersonId implements Serializable {
String firstName; String firstName;
String lastName; String lastName;
public PersonId() {
}
public PersonId(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
} }