@Override public <T> T unenhanceObject(T object) { if (object instanceof HibernateProxy) { HibernateProxy hibernateProxy = (HibernateProxy)object; LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer(); return (T)lazyInitializer.getImplementation(); } return object; }
/** * Unproxies a {@link HibernateProxy}. If the proxy is uninitialized, it automatically triggers an initialization. * In case the supplied object is null or not a proxy, the object will be returned as-is. * * @param proxy the {@link HibernateProxy} to be unproxied * @return the proxy's underlying implementation object, or the supplied object otherwise */ public static Object unproxy(Object proxy) { if ( proxy instanceof HibernateProxy ) { HibernateProxy hibernateProxy = (HibernateProxy) proxy; LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer(); return initializer.getImplementation(); } else { return proxy; } }
/** * Get the true, underlying class of a proxied persistent class. This operation * will initialize a proxy by side-effect. * * @param proxy a persistable object or proxy * @return the true class of the instance * @throws HibernateException */ public static Class getClass(Object proxy) { if ( proxy instanceof HibernateProxy ) { return ( (HibernateProxy) proxy ).getHibernateLazyInitializer() .getImplementation() .getClass(); } else { return proxy.getClass(); } }
/** * <p>Ensure a domain object is an actual persisted object and not a Hibernate proxy object by getting its real implementation. * * <p>This is primarily useful when retrieving a lazy loaded object that has been subclassed and you have the intention of casting it. * * @param t the domain object to deproxy * @return the actual persisted object or the passed in object if it is not a Hibernate proxy */ public static <T> T deproxy(T t) { if (t instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy)t; LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer(); return (T)lazyInitializer.getImplementation(); } return t; } }
@Override public String bestGuessEntityName(Object object) { if ( object instanceof HibernateProxy ) { object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation(); } return guessEntityName( object ); }
@Override public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException { if ( maybeProxy instanceof HibernateProxy ) { final HibernateProxy proxy = (HibernateProxy) maybeProxy; final LazyInitializer li = proxy.getHibernateLazyInitializer(); reassociateProxy( li, proxy ); //initialize + unwrap the object and return it return li.getImplementation(); } else { return maybeProxy; } }
private static <T> T _unproxy(T entity) { if (Cls.exists("org.hibernate.proxy.HibernateProxy") && entity instanceof HibernateProxy) { entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation(); } return entity; }
public boolean contains(Object collection, Object childObject, SharedSessionContractImplementor session) { // we do not have to worry about queued additions to uninitialized // collections, since they can only occur for inverse collections! Iterator elems = getElementsIterator( collection, session ); while ( elems.hasNext() ) { Object element = elems.next(); // worrying about proxies is perhaps a little bit of overkill here... if ( element instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) element ).getHibernateLazyInitializer(); if ( !li.isUninitialized() ) { element = li.getImplementation(); } } if ( element == childObject ) { return true; } } return false; }
@Override public Object unproxy(Object maybeProxy) throws HibernateException { if ( maybeProxy instanceof HibernateProxy ) { final HibernateProxy proxy = (HibernateProxy) maybeProxy; final LazyInitializer li = proxy.getHibernateLazyInitializer(); if ( li.isUninitialized() ) { throw new PersistentObjectException( "object was an uninitialized proxy for " + li.getEntityName() ); } //unwrap the object and return return li.getImplementation(); } else { return maybeProxy; } }
public static Object getTargetFromProxy(SessionFactoryImplementor sessionFactoryImplementor, HibernateProxy proxy) { if ( !proxy.getHibernateLazyInitializer().isUninitialized() || activeProxySession( proxy ) ) { return proxy.getHibernateLazyInitializer().getImplementation(); } final SharedSessionContractImplementor sessionImplementor = proxy.getHibernateLazyInitializer().getSession(); final Session tempSession = sessionImplementor == null ? sessionFactoryImplementor.openTemporarySession() : sessionImplementor.getFactory().openTemporarySession(); try { return tempSession.get( proxy.getHibernateLazyInitializer().getEntityName(), proxy.getHibernateLazyInitializer().getIdentifier() ); } finally { tempSession.close(); } }
@Override public String bestGuessEntityName(Object object) { if ( object instanceof HibernateProxy ) { LazyInitializer initializer = ( (HibernateProxy) object ).getHibernateLazyInitializer(); // it is possible for this method to be called during flush processing, // so make certain that we do not accidentally initialize an uninitialized proxy if ( initializer.isUninitialized() ) { return initializer.getEntityName(); } object = initializer.getImplementation(); } EntityEntry entry = persistenceContext.getEntry( object ); if ( entry == null ) { return guessEntityName( object ); } else { return entry.getPersister().getEntityName(); } }
@Override public String getEntityName(Object object) { checkOpen(); // checkTransactionSynchStatus(); if ( object instanceof HibernateProxy ) { if ( !persistenceContext.containsProxy( object ) ) { throw new TransientObjectException( "proxy was not associated with the session" ); } object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation(); } EntityEntry entry = persistenceContext.getEntry( object ); if ( entry == null ) { throwTransientObjectException( object ); } return entry.getPersister().getEntityName(); }
private EntityPersister guessEntityPersister(Object object) { if ( scope == null ) { return null; } String entityName = null; // this code is largely copied from Session's bestGuessEntityName Object entity = object; if ( entity instanceof HibernateProxy ) { final LazyInitializer initializer = ( (HibernateProxy) entity ).getHibernateLazyInitializer(); if ( initializer.isUninitialized() ) { entityName = initializer.getEntityName(); } entity = initializer.getImplementation(); } if ( entityName == null ) { for ( EntityNameResolver resolver : scope.getTypeConfiguration().getSessionFactory().getMetamodel().getEntityNameResolvers() ) { entityName = resolver.resolveEntityName( entity ); if ( entityName != null ) { break; } } } if ( entityName == null ) { // the old-time stand-by... entityName = object.getClass().getName(); } return scope.getTypeConfiguration().getSessionFactory().getMetamodel().entityPersister( entityName ); }
@Override public LockMode getCurrentLockMode(Object object) throws HibernateException { checkOpen(); checkTransactionSynchStatus(); if ( object == null ) { throw new NullPointerException( "null object passed to getCurrentLockMode()" ); } if ( object instanceof HibernateProxy ) { object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation( this ); if ( object == null ) { return LockMode.NONE; } } EntityEntry e = persistenceContext.getEntry( object ); if ( e == null ) { throw new TransientObjectException( "Given object not associated with the session" ); } if ( e.getStatus() != Status.MANAGED ) { throw new ObjectDeletedException( "The given object was deleted", e.getId(), e.getPersister().getEntityName() ); } return e.getLockMode(); }
@Override public String getEntityName(Object primaryKey, Number revision, Object entity) throws HibernateException { checkNotNull( primaryKey, "Primary key" ); checkNotNull( revision, "Entity revision" ); checkPositive( revision, "Entity revision" ); checkNotNull( entity, "Entity" ); checkSession(); // Unwrap if necessary if ( entity instanceof HibernateProxy ) { entity = ( (HibernateProxy) entity ).getHibernateLazyInitializer().getImplementation(); } if ( firstLevelCache.containsEntityName( primaryKey, revision, entity ) ) { // it's on envers FLC! return firstLevelCache.getFromEntityNameCache( primaryKey, revision, entity ); } else { throw new HibernateException( "Envers can't resolve entityName for historic entity. The id, revision and entity is not on envers first level cache." ); } } }
private void checkObject(Object entityOrProxy, Set expectedInitializedObjects, Set expectedReadOnlyObjects, Session s) { boolean isExpectedToBeInitialized = expectedInitializedObjects.contains( entityOrProxy ); boolean isExpectedToBeReadOnly = expectedReadOnlyObjects.contains( entityOrProxy ); SessionImplementor si = (SessionImplementor) s; assertEquals( isExpectedToBeInitialized, Hibernate.isInitialized( entityOrProxy ) ); assertEquals( isExpectedToBeReadOnly, s.isReadOnly( entityOrProxy ) ); if ( Hibernate.isInitialized( entityOrProxy ) ) { Object entity = ( entityOrProxy instanceof HibernateProxy ? ( ( HibernateProxy ) entityOrProxy ).getHibernateLazyInitializer().getImplementation( si ) : entityOrProxy ); assertNotNull( entity ); assertEquals( isExpectedToBeReadOnly, s.isReadOnly( entity )); } }
@Override public void setReadOnly(Object object, boolean readOnly) { if ( object == null ) { throw new AssertionFailure( "object must be non-null." ); } if ( isReadOnly( object ) == readOnly ) { return; } if ( object instanceof HibernateProxy ) { final HibernateProxy proxy = (HibernateProxy) object; setProxyReadOnly( proxy, readOnly ); if ( Hibernate.isInitialized( proxy ) ) { setEntityReadOnly( proxy.getHibernateLazyInitializer().getImplementation(), readOnly ); } } else { setEntityReadOnly( object, readOnly ); // PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity // so need to check the return value to be sure it is really a proxy final Object maybeProxy = getSession().getPersistenceContext().proxyFor( object ); if ( maybeProxy instanceof HibernateProxy ) { setProxyReadOnly( (HibernateProxy) maybeProxy, readOnly ); } } }
public void unScheduleDeletion(EntityEntry entry, Object rescuedEntity) { if ( rescuedEntity instanceof HibernateProxy ) { LazyInitializer initializer = ( (HibernateProxy) rescuedEntity ).getHibernateLazyInitializer(); if ( !initializer.isUninitialized() ) { rescuedEntity = initializer.getImplementation( session ); } } if( deletions != null ) { for ( int i = 0; i < deletions.size(); i++ ) { EntityDeleteAction action = deletions.get( i ); if (action.getInstance() == rescuedEntity) { deletions.remove( i ); return; } } } if( orphanRemovals != null ) { for ( int i = 0; i < orphanRemovals.size(); i++ ) { EntityDeleteAction action = orphanRemovals.get( i ); if (action.getInstance() == rescuedEntity) { orphanRemovals.remove( i ); return; } } } throw new AssertionFailure( "Unable to perform un-delete for instance " + entry.getEntityName() ); }
private void checkProxyReadOnly(Session s, Object proxy, boolean expectedReadOnly) { assertTrue( proxy instanceof HibernateProxy ); LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); assertSame( s, li.getSession() ); assertEquals( expectedReadOnly, s.isReadOnly( proxy ) ); assertEquals( expectedReadOnly, li.isReadOnly() ); assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() ); if ( Hibernate.isInitialized( proxy ) ) { assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) ); } }
private void checkReadOnly(Session s, Object proxy, boolean expectedReadOnly) { assertTrue( proxy instanceof HibernateProxy ); LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); assertSame( s, li.getSession() ); assertEquals( expectedReadOnly, s.isReadOnly( proxy ) ); assertEquals( expectedReadOnly, li.isReadOnly() ); assertEquals( Hibernate.isInitialized( proxy ), ! li.isUninitialized() ); if ( Hibernate.isInitialized( proxy ) ) { assertEquals( expectedReadOnly, s.isReadOnly( li.getImplementation() ) ); } } }