private Serializable getProxyIdentifier(Object proxy) { return ( (HibernateProxy) proxy ).getHibernateLazyInitializer().getIdentifier(); }
/** * Get the class of an instance or the underlying class * of a proxy (without initializing the proxy!). It is * almost always better to use the entity name! */ public static Class getClassWithoutInitializingProxy(Object object) { if (object instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy) object; LazyInitializer li = proxy.getHibernateLazyInitializer(); return li.getPersistentClass(); } else { return object.getClass(); } }
@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; }
@Override public String bestGuessEntityName(Object object) { if ( object instanceof HibernateProxy ) { object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation(); } return guessEntityName( object ); }
private void setProxyReadOnly(HibernateProxy proxy, boolean readOnly) { if ( proxy.getHibernateLazyInitializer().getSession() != getSession() ) { throw new AssertionFailure( "Attempt to set a proxy to read-only that is associated with a different session" ); } proxy.getHibernateLazyInitializer().setReadOnly( readOnly ); }
/** * Deserialization hook. This method is called by JDK deserialization. We use this hook * to replace the serial form with a live form. * * @return The live form. */ private Object readResolve() { HibernateProxy proxy = JavassistProxyFactory.deserializeProxy( this ); afterDeserialization( ( JavassistLazyInitializer ) proxy.getHibernateLazyInitializer() ); return proxy; } }
@Override public boolean reassociateIfUninitializedProxy(Object value) throws MappingException { if ( !Hibernate.isInitialized( value ) ) { final HibernateProxy proxy = (HibernateProxy) value; final LazyInitializer li = proxy.getHibernateLazyInitializer(); reassociateProxy( li, proxy ); return true; } else { return false; } }
@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; }
@Override public void reassociateProxy(Object value, Serializable id) throws MappingException { if ( value instanceof HibernateProxy ) { LOG.debugf( "Setting proxy identifier: %s", id ); final HibernateProxy proxy = (HibernateProxy) value; final LazyInitializer li = proxy.getHibernateLazyInitializer(); li.setIdentifier( id ); reassociateProxy( li, proxy ); } }
private Object readResolve() { BytecodeProvider bytecodeProvider = Environment.getBytecodeProvider(); if ( !( bytecodeProvider instanceof BytecodeProviderImpl ) ) { throw new IllegalStateException( "The bytecode provider is not ByteBuddy, unable to deserialize a ByteBuddy proxy." ); } HibernateProxy proxy = ( (BytecodeProviderImpl) bytecodeProvider ).getByteBuddyProxyHelper().deserializeProxy( this ); afterDeserialization( (ByteBuddyInterceptor) proxy.getHibernateLazyInitializer() ); return proxy; } }
public static Object getIdentifier(SessionImplementor session, String entityName, Object obj) { if ( obj == null ) { return null; } if ( obj instanceof HibernateProxy ) { final HibernateProxy hibernateProxy = (HibernateProxy) obj; return hibernateProxy.getHibernateLazyInitializer().getIdentifier(); } return session.getEntityPersister( entityName, obj ).getIdentifier( obj, session ); }
private static boolean activeProxySession(HibernateProxy proxy) { final Session session = (Session) proxy.getHibernateLazyInitializer().getSession(); return session != null && session.isOpen() && session.isConnected(); }
private Number getRevisionNumber(Map versionsEntity) { AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration(); String originalId = verEntCfg.getOriginalIdPropName(); String revisionPropertyName = verEntCfg.getRevisionFieldName(); Object revisionInfoObject = ( (Map) versionsEntity.get( originalId ) ).get( revisionPropertyName ); if ( revisionInfoObject instanceof HibernateProxy ) { return (Number) ( (HibernateProxy) revisionInfoObject ).getHibernateLazyInitializer().getIdentifier(); } else { // Not a proxy - must be read from cache or with a join return enversService.getRevisionInfoNumberReader().getRevisionNumber( revisionInfoObject ); } }
/** * Associate a proxy that was instantiated by another session with this session * * @param li The proxy initializer. * @param proxy The proxy to reassociate. */ private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) { if ( li.getSession() != this.getSession() ) { final EntityPersister persister = session.getFactory().getMetamodel().entityPersister( li.getEntityName() ); final EntityKey key = session.generateEntityKey( li.getIdentifier(), persister ); // any earlier proxy takes precedence proxiesByKey.putIfAbsent( key, proxy ); proxy.getHibernateLazyInitializer().setSession( session ); } }
@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 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 )); } }
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() ) ); } } }
@Test public void testProxyIdentifier() { TargetNotAuditedEntity rev1 = getAuditReader().find( TargetNotAuditedEntity.class, tnae1.getId(), 1 ); Assert.assertTrue( rev1.getReference() instanceof HibernateProxy ); HibernateProxy proxyCreateByEnvers = (HibernateProxy) rev1.getReference(); LazyInitializer lazyInitializer = proxyCreateByEnvers.getHibernateLazyInitializer(); Assert.assertTrue( lazyInitializer.isUninitialized() ); Assert.assertNotNull( lazyInitializer.getIdentifier() ); Assert.assertEquals( tnae1.getId(), lazyInitializer.getIdentifier() ); Assert.assertTrue( lazyInitializer.isUninitialized() ); Assert.assertEquals( uste1.getId(), rev1.getReference().getId() ); Assert.assertEquals( uste1.getStr(), rev1.getReference().getStr() ); Assert.assertFalse( lazyInitializer.isUninitialized() ); }