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() ) {
// 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;
}

View File

@ -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();
}

View File

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

View File

@ -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;
}
}

View File

@ -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;
}
}