HHH-10664 - Prep 6.0 feature branch - merge hibernate-entitymanager into hibernate-core (continued fixing of hibernate-core test failures)

This commit is contained in:
Steve Ebersole 2016-04-27 09:10:56 -05:00
parent 49514b92e3
commit 16c78f0438
4 changed files with 88 additions and 12 deletions

View File

@ -251,12 +251,15 @@ public interface Session extends SharedSessionContract, EntityManager, Hibernate
Serializable getIdentifier(Object object); Serializable getIdentifier(Object object);
/** /**
* Check if this instance is associated with this <tt>Session</tt>. * Check if this entity is associated with this Session. This form caters to
* non-POJO entities, by allowing the entity-name to be passed in
* *
* @param entityName The entity name
* @param object an instance of a persistent class * @param object an instance of a persistent class
*
* @return true if the given instance is associated with this <tt>Session</tt> * @return true if the given instance is associated with this <tt>Session</tt>
*/ */
boolean contains(Object object); boolean contains(String entityName, Object object);
/** /**
* Remove this instance from the session cache. Changes to the instance will * Remove this instance from the session cache. Changes to the instance will

View File

@ -719,6 +719,11 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
return delegate.getIdentifier( object ); return delegate.getIdentifier( object );
} }
@Override
public boolean contains(String entityName, Object object) {
return delegate.contains( entityName, object );
}
@Override @Override
public boolean contains(Object object) { public boolean contains(Object object) {
return delegate.contains( object ); return delegate.contains( object );

View File

@ -108,7 +108,7 @@ public class ForeignGenerator implements IdentifierGenerator, Configurable {
id = session.save( foreignValueSourceType.getAssociatedEntityName(), associatedObject ); id = session.save( foreignValueSourceType.getAssociatedEntityName(), associatedObject );
} }
if ( session.contains(object) ) { if ( session.contains( entityName, object ) ) {
//abort the save (the object is already saved by a circular cascade) //abort the save (the object is already saved by a circular cascade)
return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR; return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR;
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association"); //throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");

View File

@ -1885,19 +1885,87 @@ public final class SessionImpl
@Override @Override
public boolean contains(Object object) { public boolean contains(Object object) {
checkOpen(); checkOpen();
// checkTransactionSynchStatus(); checkTransactionSynchStatus();
if ( object == null ) {
return false;
}
if ( object != null && !HibernateProxy.class.isInstance( object ) && persistenceContext.getEntry( object ) == null ) {
// check if it is an entity -> if not throw an exception (per JPA)
try { try {
if ( object instanceof HibernateProxy ) {
//do not use proxiesByKey, since not all
//proxies that point to this session's
//instances are in that collection!
LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
if ( li.isUninitialized() ) {
//if it is an uninitialized proxy, pointing
//with this session, then when it is accessed,
//the underlying instance will be "contained"
return li.getSession() == this;
}
else {
//if it is initialized, see if the underlying
//instance is contained, since we need to
//account for the fact that it might have been
//evicted
object = li.getImplementation();
}
}
// A session is considered to contain an entity only if the entity has
// an entry in the session's persistence context and the entry reports
// that the entity has not been removed
EntityEntry entry = persistenceContext.getEntry( object );
delayedAfterCompletion();
if ( entry == null ) {
if ( !HibernateProxy.class.isInstance( object ) && persistenceContext.getEntry( object ) == null ) {
// check if it is even an entity -> if not throw an exception (per JPA)
try {
final String entityName = getEntityNameResolver().resolveEntityName( object );
if ( entityName == null ) {
throw new IllegalArgumentException( "Could not resolve entity-name [" + object + "]" );
}
getSessionFactory().getMetamodel().entityPersister( object.getClass() ); getSessionFactory().getMetamodel().entityPersister( object.getClass() );
} }
catch (HibernateException e) { catch (HibernateException e) {
throw convert( new IllegalArgumentException( "Not an entity:" + object.getClass() ) ); throw new IllegalArgumentException( "Not an entity [" + object.getClass() + "]", e );
}
}
return false;
}
else {
return entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
}
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (RuntimeException e) {
throw convert( e );
} }
} }
@Override
public boolean contains(String entityName, Object object) {
checkOpen();
checkTransactionSynchStatus();
if ( object == null ) {
return false;
}
try { try {
if ( !HibernateProxy.class.isInstance( object ) && persistenceContext.getEntry( object ) == null ) {
// check if it is an entity -> if not throw an exception (per JPA)
try {
getSessionFactory().getMetamodel().entityPersister( entityName );
}
catch (HibernateException e) {
throw new IllegalArgumentException( "Not an entity [" + entityName + "] : " + object );
}
}
if ( object instanceof HibernateProxy ) { if ( object instanceof HibernateProxy ) {
//do not use proxiesByKey, since not all //do not use proxiesByKey, since not all
//proxies that point to this session's //proxies that point to this session's