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:
parent
49514b92e3
commit
16c78f0438
|
@ -251,12 +251,15 @@ public interface Session extends SharedSessionContract, EntityManager, Hibernate
|
|||
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
|
||||
*
|
||||
* @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
|
||||
|
|
|
@ -719,6 +719,11 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
|
|||
return delegate.getIdentifier( object );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String entityName, Object object) {
|
||||
return delegate.contains( entityName, object );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object object) {
|
||||
return delegate.contains( object );
|
||||
|
|
|
@ -108,7 +108,7 @@ public class ForeignGenerator implements IdentifierGenerator, Configurable {
|
|||
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)
|
||||
return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR;
|
||||
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
|
||||
|
|
|
@ -1885,19 +1885,87 @@ public final class SessionImpl
|
|||
@Override
|
||||
public boolean contains(Object object) {
|
||||
checkOpen();
|
||||
// checkTransactionSynchStatus();
|
||||
checkTransactionSynchStatus();
|
||||
|
||||
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 {
|
||||
getSessionFactory().getMetamodel().entityPersister( object.getClass() );
|
||||
}
|
||||
catch (HibernateException e) {
|
||||
throw convert( new IllegalArgumentException( "Not an entity:" + object.getClass() ) );
|
||||
}
|
||||
if ( object == null ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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() );
|
||||
}
|
||||
catch (HibernateException e) {
|
||||
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 {
|
||||
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 ) {
|
||||
//do not use proxiesByKey, since not all
|
||||
//proxies that point to this session's
|
||||
|
|
Loading…
Reference in New Issue