下面列出了org.hibernate.engine.SessionImplementor#getEntityMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static EntityKey getOptionalObjectKey(QueryParameters queryParameters, SessionImplementor session) {
final Object optionalObject = queryParameters.getOptionalObject();
final Serializable optionalId = queryParameters.getOptionalId();
final String optionalEntityName = queryParameters.getOptionalEntityName();
if ( optionalObject != null && optionalEntityName != null ) {
return new EntityKey(
optionalId,
session.getEntityPersister( optionalEntityName, optionalObject ),
session.getEntityMode()
);
}
else {
return null;
}
}
private Object getReplacement() {
final SessionImplementor session = getSession();
if ( isUninitialized() && session != null && session.isOpen()) {
final EntityKey key = new EntityKey(
getIdentifier(),
session.getFactory().getEntityPersister( getEntityName() ),
session.getEntityMode()
);
final Object entity = session.getPersistenceContext().getEntity(key);
if (entity!=null) setImplementation( entity );
}
if ( isUninitialized() ) {
if (replacement==null) {
replacement = serializableProxy();
}
return replacement;
}
else {
return getTarget();
}
}
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) {
if ( persister.isSelectBeforeUpdateRequired() ) {
Object[] snapshot = session.getPersistenceContext()
.getDatabaseSnapshot(id, persister);
if (snapshot==null) {
//do we even really need this? the update will fail anyway....
if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
session.getFactory().getStatisticsImplementor()
.optimisticFailure( persister.getEntityName() );
}
throw new StaleObjectStateException( persister.getEntityName(), id );
}
else {
return snapshot;
}
}
else {
//TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
EntityKey entityKey = new EntityKey( id, persister, session.getEntityMode() );
return session.getPersistenceContext()
.getCachedDatabaseSnapshot( entityKey );
}
}
public PersistentCollection wrap(SessionImplementor session, Object collection) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
return new PersistentMapElementHolder( session, (Element) collection );
}
else {
return new PersistentMap( session, (java.util.Map) collection );
}
}
public PersistentCollection wrap(SessionImplementor session, Object collection) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
return new PersistentElementHolder( session, (Element) collection );
}
else {
return new PersistentSet( session, (java.util.Set) collection );
}
}
public Object initializeLazyProperty(String fieldName, Object entity, SessionImplementor session)
throws HibernateException {
final Serializable id = session.getContextEntityIdentifier( entity );
final EntityEntry entry = session.getPersistenceContext().getEntry( entity );
if ( entry == null ) {
throw new HibernateException( "entity is not associated with the session: " + id );
}
if ( log.isTraceEnabled() ) {
log.trace(
"initializing lazy properties of: " +
MessageHelper.infoString( this, id, getFactory() ) +
", field access: " + fieldName
);
}
if ( hasCache() ) {
CacheKey cacheKey = new CacheKey(id, getIdentifierType(), getEntityName(), session.getEntityMode(), getFactory() );
Object ce = getCache().get( cacheKey, session.getTimestamp() );
if (ce!=null) {
CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure(ce, factory);
if ( !cacheEntry.areLazyPropertiesUnfetched() ) {
//note early exit here:
return initializeLazyPropertiesFromCache( fieldName, entity, session, entry, cacheEntry );
}
}
}
return initializeLazyPropertiesFromDatastore( fieldName, entity, session, id, entry );
}
/**
* Delete an object
*/
public void delete(Serializable id, Object version, Object object, SessionImplementor session)
throws HibernateException {
final int span = getTableSpan();
boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned() && entityMetamodel.getOptimisticLockMode() > Versioning.OPTIMISTIC_LOCK_VERSION;
Object[] loadedState = null;
if ( isImpliedOptimisticLocking ) {
// need to treat this as if it where optimistic-lock="all" (dirty does *not* make sense);
// first we need to locate the "loaded" state
//
// Note, it potentially could be a proxy, so perform the location the safe way...
EntityKey key = new EntityKey( id, this, session.getEntityMode() );
Object entity = session.getPersistenceContext().getEntity( key );
if ( entity != null ) {
EntityEntry entry = session.getPersistenceContext().getEntry( entity );
loadedState = entry.getLoadedState();
}
}
final String[] deleteStrings;
if ( isImpliedOptimisticLocking && loadedState != null ) {
// we need to utilize dynamic delete statements
deleteStrings = generateSQLDeletStrings( loadedState );
}
else {
// otherwise, utilize the static delete statements
deleteStrings = getSQLDeleteStrings();
}
for ( int j = span - 1; j >= 0; j-- ) {
delete( id, version, j, object, deleteStrings[j], session, loadedState );
}
}
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
return new PersistentListElementHolder(session, persister, key);
}
else {
return new PersistentList(session);
}
}
@Override
public PersistentCollection wrap(SessionImplementor session, Object collection) {
if ( session.getEntityMode() == org.hibernate.EntityMode.DOM4J ) {
throw new IllegalStateException("dom4j not supported");
}
else {
return new utils.PersistentOwnedList( session, (java.util.List) collection );
}
}
@Override
public PersistentCollection wrap(SessionImplementor session, Object collection) {
if ( session.getEntityMode() == org.hibernate.EntityMode.DOM4J ) {
throw new IllegalStateException("dom4j not supported");
}
else {
return new utils.PersistentOwnedSet( session, (java.util.Set) collection );
}
}
public PersistentCollection wrap(SessionImplementor session, Object collection) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
throw new IllegalStateException("dom4j not supported");
}
else {
return new PersistentMyList( session, (IMyList) collection );
}
}
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
return new PersistentMapElementHolder(session, persister, key);
}
else {
PersistentSortedMap map = new PersistentSortedMap(session);
map.setComparator(comparator);
return map;
}
}
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
return new PersistentElementHolder(session, persister, key);
}
else {
PersistentSortedSet set = new PersistentSortedSet(session);
set.setComparator(comparator);
return set;
}
}
public PersistentCollection wrap(SessionImplementor session, Object collection) {
if ( session.getEntityMode()==EntityMode.DOM4J ) {
return new PersistentElementHolder( session, (Element) collection );
}
else {
return new PersistentBag( session, (Collection) collection );
}
}
/**
* Add the collection to the second-level cache
*
* @param lce The entry representing the collection to add
* @param persister The persister
*/
private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersister persister) {
final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
final SessionFactoryImplementor factory = session.getFactory();
if ( log.isDebugEnabled() ) {
log.debug( "Caching collection: " + MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) );
}
if ( !session.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( session ) ) {
// some filters affecting the collection are enabled on the session, so do not do the put into the cache.
log.debug( "Refusing to add to cache due to enabled filters" );
// todo : add the notion of enabled filters to the CacheKey to differentiate filtered collections from non-filtered;
// but CacheKey is currently used for both collections and entities; would ideally need to define two seperate ones;
// currently this works in conjuction with the check on
// DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
// cache with enabled filters).
return; // EARLY EXIT!!!!!
}
final Comparator versionComparator;
final Object version;
if ( persister.isVersioned() ) {
versionComparator = persister.getOwnerEntityPersister().getVersionType().getComparator();
final Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
}
else {
version = null;
versionComparator = null;
}
CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
CacheKey cacheKey = new CacheKey(
lce.getKey(),
persister.getKeyType(),
persister.getRole(),
session.getEntityMode(),
session.getFactory()
);
boolean put = persister.getCache().put(
cacheKey,
persister.getCacheEntryStructure().structure(entry),
session.getTimestamp(),
version,
versionComparator,
factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
);
if ( put && factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor().secondLevelCachePut( persister.getCache().getRegionName() );
}
}
/**
* Performs a pessimistic lock upgrade on a given entity, if needed.
*
* @param object The entity for which to upgrade the lock.
* @param entry The entity's EntityEntry instance.
* @param requestedLockMode The lock mode being requested for locking.
* @param source The session which is the source of the event being processed.
* @throws HibernateException
*/
protected void upgradeLock(Object object, EntityEntry entry, LockMode requestedLockMode, SessionImplementor source)
throws HibernateException {
if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) {
// The user requested a "greater" (i.e. more restrictive) form of
// pessimistic lock
if ( entry.getStatus() != Status.MANAGED ) {
throw new ObjectDeletedException(
"attempted to lock a deleted instance",
entry.getId(),
entry.getPersister().getEntityName()
);
}
final EntityPersister persister = entry.getPersister();
if ( log.isTraceEnabled() )
log.trace(
"locking " +
MessageHelper.infoString( persister, entry.getId(), source.getFactory() ) +
" in mode: " +
requestedLockMode
);
final CacheConcurrencyStrategy.SoftLock lock;
final CacheKey ck;
if ( persister.hasCache() ) {
ck = new CacheKey(
entry.getId(),
persister.getIdentifierType(),
persister.getRootEntityName(),
source.getEntityMode(),
source.getFactory()
);
lock = persister.getCache().lock( ck, entry.getVersion() );
}
else {
ck = null;
lock = null;
}
try {
if ( persister.isVersioned() && requestedLockMode == LockMode.FORCE ) {
// todo : should we check the current isolation mode explicitly?
Object nextVersion = persister.forceVersionIncrement(
entry.getId(), entry.getVersion(), source
);
entry.forceLocked( object, nextVersion );
}
else {
persister.lock( entry.getId(), entry.getVersion(), object, requestedLockMode, source );
}
entry.setLockMode(requestedLockMode);
}
finally {
// the database now holds a lock + the object is flushed from the cache,
// so release the soft lock
if ( persister.hasCache() ) {
persister.getCache().release(ck, lock );
}
}
}
}
public boolean isNull(Object owner, SessionImplementor session) {
if ( propertyName != null ) {
EntityPersister ownerPersister = session.getFactory()
.getEntityPersister(entityName);
Serializable id = session.getContextEntityIdentifier(owner);
EntityKey entityKey = new EntityKey( id, ownerPersister, session.getEntityMode() );
return session.getPersistenceContext()
.isPropertyNull( entityKey, getPropertyName() );
}
else {
return false;
}
}
protected boolean isNotEmbedded(SessionImplementor session) {
return !isEmbeddedInXML && session.getEntityMode()==EntityMode.DOM4J;
}
/**
* Attempts to load the entity from the second-level cache.
*
* @param event The load event
* @param persister The persister for the entity being requested for load
* @param options The load options.
* @return The entity from the second-level cache, or null.
* @throws HibernateException
*/
protected Object loadFromSecondLevelCache(
final LoadEvent event,
final EntityPersister persister,
final LoadEventListener.LoadType options) throws HibernateException {
final SessionImplementor source = event.getSession();
final boolean useCache = persister.hasCache() &&
source.getCacheMode().isGetEnabled() &&
event.getLockMode().lessThan(LockMode.READ);
if (useCache) {
final SessionFactoryImplementor factory = source.getFactory();
final CacheKey ck = new CacheKey(
event.getEntityId(),
persister.getIdentifierType(),
persister.getRootEntityName(),
source.getEntityMode(),
source.getFactory()
);
Object ce = persister.getCache()
.get( ck, source.getTimestamp() );
if ( factory.getStatistics().isStatisticsEnabled() ) {
if (ce==null) {
factory.getStatisticsImplementor().secondLevelCacheMiss(
persister.getCache().getRegionName()
);
}
else {
factory.getStatisticsImplementor().secondLevelCacheHit(
persister.getCache().getRegionName()
);
}
}
if ( ce != null ) {
CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure()
.destructure(ce, factory);
// Entity was found in second-level cache...
return assembleCacheEntry(
entry,
event.getEntityId(),
persister,
event
);
}
}
return null;
}
public void execute() throws HibernateException {
Serializable id = getId();
EntityPersister persister = getPersister();
SessionImplementor session = getSession();
Object instance = getInstance();
boolean veto = preDelete();
Object version = this.version;
if ( persister.isVersionPropertyGenerated() ) {
// we need to grab the version value from the entity, otherwise
// we have issues with generated-version entities that may have
// multiple actions queued during the same flush
version = persister.getVersion( instance, session.getEntityMode() );
}
final CacheKey ck;
if ( persister.hasCache() ) {
ck = new CacheKey(
id,
persister.getIdentifierType(),
persister.getRootEntityName(),
session.getEntityMode(),
session.getFactory()
);
lock = persister.getCache().lock(ck, version);
}
else {
ck = null;
}
if ( !isCascadeDeleteEnabled && !veto ) {
persister.delete( id, version, instance, session );
}
//postDelete:
// After actually deleting a row, record the fact that the instance no longer
// exists on the database (needed for identity-column key generation), and
// remove it from the session cache
final PersistenceContext persistenceContext = session.getPersistenceContext();
EntityEntry entry = persistenceContext.removeEntry( instance );
if ( entry == null ) {
throw new AssertionFailure( "possible nonthreadsafe access to session" );
}
entry.postDelete();
EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() );
persistenceContext.removeEntity(key);
persistenceContext.removeProxy(key);
if ( persister.hasCache() ) persister.getCache().evict(ck);
postDelete();
if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
getSession().getFactory().getStatisticsImplementor()
.deleteEntity( getPersister().getEntityName() );
}
}