org.hibernate.proxy.HibernateProxy Java Examples
The following examples show how to use
org.hibernate.proxy.HibernateProxy.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateMapper.java From projectforge-webapp with GNU General Public License v3.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public String serializedClass(Class clazz) { // check whether we are hibernate proxy and substitute real name for (int i = 0; i < clazz.getInterfaces().length; i++) { if (HibernateProxy.class.equals(clazz.getInterfaces()[i])) { // System.err.println("resolving to class name:" + clazz.getSuperclass().getName()); return clazz.getSuperclass().getName(); } } if (collectionMap.containsKey(clazz)) { // System.err.println("** substituting " + clazz + " with " + collectionMap.get(clazz)); return ((Class) collectionMap.get(clazz)).getName(); } return super.serializedClass(clazz); }
Example #2
Source File: EntityType.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * {@inheritDoc} */ public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor factory) { EntityPersister persister = factory.getEntityPersister(associatedEntityName); if ( !persister.canExtractIdOutOfEntity() ) { return super.getHashCode(x, entityMode); } final Serializable id; if (x instanceof HibernateProxy) { id = ( (HibernateProxy) x ).getHibernateLazyInitializer().getIdentifier(); } else { id = persister.getIdentifier(x, entityMode); } return persister.getIdentifierType().getHashCode(id, entityMode, factory); }
Example #3
Source File: GrailsHibernateUtil.java From gorm-hibernate5 with Apache License 2.0 | 6 votes |
/** * Sets the target object to read-write, allowing Hibernate to dirty check it and auto-flush changes. * * @see #setObjectToReadyOnly(Object, org.hibernate.SessionFactory) * * @param target The target object * @param sessionFactory The SessionFactory instance */ public static void setObjectToReadWrite(final Object target, SessionFactory sessionFactory) { Session session = sessionFactory.getCurrentSession(); if (!canModifyReadWriteState(session, target)) { return; } SessionImplementor sessionImpl = (SessionImplementor) session; EntityEntry ee = sessionImpl.getPersistenceContext().getEntry(target); if (ee == null || ee.getStatus() != Status.READ_ONLY) { return; } Object actualTarget = target; if (target instanceof HibernateProxy) { actualTarget = ((HibernateProxy)target).getHibernateLazyInitializer().getImplementation(); } session.setReadOnly(actualTarget, false); session.setHibernateFlushMode(FlushMode.AUTO); incrementVersion(target); }
Example #4
Source File: HibernateProxySerializer.java From onedev with MIT License | 6 votes |
@Override public void serializeWithType(HibernateProxy value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException, JsonProcessingException { Object proxiedValue = findProxied(value); if (proxiedValue == null) { provider.defaultSerializeNull(jgen); return; } /* * This isn't exactly right, since type serializer really refers to * proxy object, not value. And we really don't either know static type * (necessary to know how to apply additional type info) or other * things; so it's not going to work well. But... we'll do out best. */ findSerializer(provider, proxiedValue).serializeWithType(proxiedValue, jgen, provider, typeSer); }
Example #5
Source File: Hibernate.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * Check if the property is initialized. If the named property does not exist * or is not persistent, this method always returns <tt>true</tt>. * * @param proxy The potential proxy * @param propertyName the name of a persistent attribute of the object * @return true if the named property of the object is not listed as uninitialized * @return false if the object is an uninitialized proxy, or the named property is uninitialized */ public static boolean isPropertyInitialized(Object proxy, String propertyName) { Object entity; if ( proxy instanceof HibernateProxy ) { LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { return false; } else { entity = li.getImplementation(); } } else { entity = proxy; } if ( FieldInterceptionHelper.isInstrumented( entity ) ) { FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor( entity ); return interceptor == null || interceptor.isInitialized( propertyName ); } else { return true; } }
Example #6
Source File: StatefulPersistenceContext.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * Get the entity instance underlying the given proxy, throwing * an exception if the proxy is uninitialized. If the given object * is not a proxy, simply return the argument. */ public Object unproxy(Object maybeProxy) throws HibernateException { if ( maybeProxy instanceof ElementWrapper ) { maybeProxy = ( (ElementWrapper) maybeProxy ).getElement(); } if ( maybeProxy instanceof HibernateProxy ) { HibernateProxy proxy = (HibernateProxy) maybeProxy; LazyInitializer li = proxy.getHibernateLazyInitializer(); if ( li.isUninitialized() ) { throw new PersistentObjectException( "object was an uninitialized proxy for " + li.getEntityName() ); } return li.getImplementation(); //unwrap the object } else { return maybeProxy; } }
Example #7
Source File: EntityType.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * Resolve an identifier via a load. * * @param id The entity id to resolve * @param session The orginating session. * @return The resolved identifier (i.e., loaded entity). * @throws org.hibernate.HibernateException Indicates problems performing the load. */ protected final Object resolveIdentifier(Serializable id, SessionImplementor session) throws HibernateException { boolean isProxyUnwrapEnabled = unwrapProxy && session.getFactory() .getEntityPersister( getAssociatedEntityName() ) .isInstrumented( session.getEntityMode() ); Object proxyOrEntity = session.internalLoad( getAssociatedEntityName(), id, eager, isNullable() && !isProxyUnwrapEnabled ); if ( proxyOrEntity instanceof HibernateProxy ) { ( ( HibernateProxy ) proxyOrEntity ).getHibernateLazyInitializer() .setUnwrap( isProxyUnwrapEnabled ); } return proxyOrEntity; }
Example #8
Source File: StatefulPersistenceContext.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * Possibly unproxy the given reference and reassociate it with the current session. * * @param maybeProxy The reference to be unproxied if it currently represents a proxy. * @return The unproxied instance. * @throws HibernateException */ public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException { if ( maybeProxy instanceof ElementWrapper ) { maybeProxy = ( (ElementWrapper) maybeProxy ).getElement(); } if ( maybeProxy instanceof HibernateProxy ) { HibernateProxy proxy = (HibernateProxy) maybeProxy; LazyInitializer li = proxy.getHibernateLazyInitializer(); reassociateProxy(li, proxy); return li.getImplementation(); //initialize + unwrap the object } else { return maybeProxy; } }
Example #9
Source File: SessionImpl.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public Serializable getIdentifier(Object object) throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); if ( object instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.getSession() != this ) { throw new TransientObjectException( "The proxy was not associated with this session" ); } return li.getIdentifier(); } else { EntityEntry entry = persistenceContext.getEntry(object); if ( entry == null ) { throw new TransientObjectException( "The instance was not associated with this session" ); } return entry.getId(); } }
Example #10
Source File: Dom4jEntityTuplizer.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
protected ProxyFactory buildProxyFactory(PersistentClass mappingInfo, Getter idGetter, Setter idSetter) { HashSet proxyInterfaces = new HashSet(); proxyInterfaces.add( HibernateProxy.class ); proxyInterfaces.add( Element.class ); ProxyFactory pf = new Dom4jProxyFactory(); try { pf.postInstantiate( getEntityName(), Element.class, proxyInterfaces, null, null, mappingInfo.hasEmbeddedIdentifier() ? (AbstractComponentType) mappingInfo.getIdentifier().getType() : null ); } catch ( HibernateException he ) { log.warn( "could not create proxy factory for:" + getEntityName(), he ); pf = null; } return pf; }
Example #11
Source File: StatefulPersistenceContext.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * If a deleted entity instance is re-saved, and it has a proxy, we need to * reset the identifier of the proxy */ public void reassociateProxy(Object value, Serializable id) throws MappingException { if ( value instanceof ElementWrapper ) { value = ( (ElementWrapper) value ).getElement(); } if ( value instanceof HibernateProxy ) { if ( log.isDebugEnabled() ) log.debug("setting proxy identifier: " + id); HibernateProxy proxy = (HibernateProxy) value; LazyInitializer li = proxy.getHibernateLazyInitializer(); li.setIdentifier(id); reassociateProxy(li, proxy); } }
Example #12
Source File: HibernateLog.java From webdsl with Apache License 2.0 | 5 votes |
protected void checkDuplicate(org.hibernate.Session session, HibernateLogEntry entry, HibernateLogEntityResult res) { Object obj = null; try{ obj = session.load(res.entity, UUID.fromString(res.id)); } catch(Exception e) { } // If load returned an uninitialized proxy then we set it to null, so that we do not use it (to prevent initialization) // However, load should never return an uninitialized proxy, because we are looking at entities that should have been hydrated if(obj instanceof HibernateProxy && ((HibernateProxy)obj).getHibernateLazyInitializer().isUninitialized()) obj = null; if(obj instanceof WebDSLEntity) { res.entity = ((WebDSLEntity)obj).get_WebDslEntityType(); // This returns the correct sub-entity } else { // This only happens if entId is not a UUID, load threw an exception or returned an uninitialized proxy. // We record it with the type shown inside the EntityKey, but the real sub-type is unknown if(res.entity.indexOf("webdsl.generated.domain.") == 0) res.entity = res.entity.substring("webdsl.generated.domain.".length()); res.entity = res.entity + " (subtype uknown)"; } if(_fetched.contains(res.entity+"#"+res.id)) { _duplicates++; if(entry != null) entry.duplicates++; if(_duplicateCounter.containsKey(res.entity)) { _duplicateCounter.put(res.entity, _duplicateCounter.get(res.entity) + 1); } else { _duplicateCounter.put(res.entity, 1); } } else { _fetched.add(res.entity+"#"+res.id); } }
Example #13
Source File: EntityType.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * {@inheritDoc} */ public boolean isEqual(Object x, Object y, EntityMode entityMode, SessionFactoryImplementor factory) { EntityPersister persister = factory.getEntityPersister(associatedEntityName); if ( !persister.canExtractIdOutOfEntity() ) { return super.isEqual(x, y, entityMode); } Serializable xid; if (x instanceof HibernateProxy) { xid = ( (HibernateProxy) x ).getHibernateLazyInitializer() .getIdentifier(); } else { xid = persister.getIdentifier(x, entityMode); } Serializable yid; if (y instanceof HibernateProxy) { yid = ( (HibernateProxy) y ).getHibernateLazyInitializer() .getIdentifier(); } else { yid = persister.getIdentifier(y, entityMode); } return persister.getIdentifierType() .isEqual(xid, yid, entityMode, factory); }
Example #14
Source File: PortfolioRepositoryHandler.java From olat with Apache License 2.0 | 5 votes |
/** * */ protected PortfolioRepositoryHandler() { supportedTypes = new ArrayList<String>(1); supportedTypes.add(EPTemplateMapResource.TYPE_NAME); myStream.alias("defaultMap", EPDefaultMap.class); myStream.alias("structureMap", EPStructuredMap.class); myStream.alias("templateMap", EPStructuredMapTemplate.class); myStream.alias("structure", EPStructureElement.class); myStream.alias("page", EPPage.class); myStream.alias("structureToArtefact", EPStructureToArtefactLink.class); myStream.alias("structureToStructure", EPStructureToStructureLink.class); myStream.alias("collectionRestriction", CollectRestriction.class); myStream.alias("org.olat.resource.OLATResourceImpl", OLATResourceImpl.class); myStream.alias("OLATResource", OLATResourceImpl.class); myStream.alias("org.olat.basesecurity.SecurityGroupImpl", SecurityGroupImpl.class); myStream.alias("SecurityGroupImpl", SecurityGroupImpl.class); myStream.alias("org.olat.basesecurity.SecurityGroup", SecurityGroup.class); myStream.alias("SecurityGroup", SecurityGroup.class); myStream.alias("org.olat.core.id.Persistable", Persistable.class); myStream.alias("Persistable", Persistable.class); myStream.alias("org.hibernate.proxy.HibernateProxy", HibernateProxy.class); myStream.alias("HibernateProxy", HibernateProxy.class); myStream.omitField(EPStructuredMapTemplate.class, "ownerGroup"); myStream.addDefaultImplementation(PersistentList.class, List.class); myStream.addDefaultImplementation(ArrayList.class, List.class); myStream.registerConverter(new CollectionConverter(myStream.getMapper()) { @Override public boolean canConvert(final Class type) { return PersistentList.class == type; } }); }
Example #15
Source File: StatefulPersistenceContext.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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() ) { EntityPersister persister = session.getFactory().getEntityPersister( li.getEntityName() ); EntityKey key = new EntityKey( li.getIdentifier(), persister, session.getEntityMode() ); // any earlier proxy takes precedence if ( !proxiesByKey.containsKey( key ) ) { proxiesByKey.put( key, proxy ); } proxy.getHibernateLazyInitializer().setSession( session ); } }
Example #16
Source File: Hibernate.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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(); } }
Example #17
Source File: Hibernate.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Check if the proxy or persistent collection is initialized. * * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> * @return true if the argument is already initialized, or is not a proxy or collection */ @SuppressWarnings("SimplifiableIfStatement") public static boolean isInitialized(Object proxy) { if ( proxy instanceof HibernateProxy ) { return !( (HibernateProxy) proxy ).getHibernateLazyInitializer().isUninitialized(); } else if ( proxy instanceof PersistentCollection ) { return ( (PersistentCollection) proxy ).wasInitialized(); } else { return true; } }
Example #18
Source File: CGLIBProxyFactory.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public HibernateProxy getProxy(Serializable id, SessionImplementor session) throws HibernateException { return CGLIBLazyInitializer.getProxy( factory, entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType, id, session ); }
Example #19
Source File: Hibernate.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Force initialization of a proxy or persistent collection. * <p/> * Note: This only ensures intialization of a proxy object or collection; * it is not guaranteed that the elements INSIDE the collection will be initialized/materialized. * * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> * @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed */ public static void initialize(Object proxy) throws HibernateException { if ( proxy == null ) { return; } if ( proxy instanceof HibernateProxy ) { ( (HibernateProxy) proxy ).getHibernateLazyInitializer().initialize(); } else if ( proxy instanceof PersistentCollection ) { ( (PersistentCollection) proxy ).forceInitialization(); } }
Example #20
Source File: HibernateSession.java From gorm-hibernate5 with Apache License 2.0 | 5 votes |
@Override public Serializable getObjectIdentifier(Object instance) { if(instance == null) return null; if(proxyHandler.isProxy(instance)) { return ((HibernateProxy)instance).getHibernateLazyInitializer().getIdentifier(); } Class<?> type = instance.getClass(); ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(type); final PersistentEntity persistentEntity = getMappingContext().getPersistentEntity(type.getName()); if(persistentEntity != null) { return (Serializable) cpf.getPropertyValue(instance, persistentEntity.getIdentity().getName()); } return null; }
Example #21
Source File: HibernateProxyXPathMarshaller.java From projectforge-webapp with GNU General Public License v3.0 | 5 votes |
@Override public void convertAnother(Object item, Converter converter) { Object toConvert; if (HibernateProxy.class.isAssignableFrom(item.getClass())) { toConvert = ((HibernateProxy) item).getHibernateLazyInitializer().getImplementation(); } else { toConvert = item; } super.convertAnother(toConvert, converter); }
Example #22
Source File: SerializableProxy.java From lams with GNU General Public License v2.0 | 5 votes |
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; }
Example #23
Source File: ByteBuddyProxyHelper.java From lams with GNU General Public License v2.0 | 5 votes |
public HibernateProxy deserializeProxy(SerializableProxy serializableProxy) { final ByteBuddyInterceptor interceptor = new ByteBuddyInterceptor( serializableProxy.getEntityName(), serializableProxy.getPersistentClass(), serializableProxy.getInterfaces(), serializableProxy.getId(), resolveIdGetterMethod( serializableProxy ), resolveIdSetterMethod( serializableProxy ), serializableProxy.getComponentIdType(), null, ReflectHelper.overridesEquals( serializableProxy.getPersistentClass() ) ); // note: interface is assumed to already contain HibernateProxy.class try { final Class proxyClass = buildProxy( serializableProxy.getPersistentClass(), serializableProxy.getInterfaces() ); final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance(); ( (ProxyConfiguration) proxy ).$$_hibernate_set_interceptor( interceptor ); return proxy; } catch (Throwable t) { final String message = LOG.bytecodeEnhancementFailed( serializableProxy.getEntityName() ); LOG.error( message, t ); throw new HibernateException( message, t ); } }
Example #24
Source File: ProxyHashSet.java From webdsl with Apache License 2.0 | 5 votes |
/** * Adds the specified element to this set if it is not already present. * More formally, adds the specified element <tt>e</tt> to this set if * this set contains no element <tt>e2</tt> such that * <tt>(e==null ? e2==null : e.equals(e2))</tt>. * If this set already contains the element, the call leaves the set * unchanged and returns <tt>false</tt>. * * @param e element to be added to this set * @return <tt>true</tt> if this set did not already contain the specified * element */ @Override public boolean add(E e) { if(e == null) { // Can't call getId on null, so always return false return false; } else if(e instanceof HibernateProxy) { return map.put((UUID)((HibernateProxy)e).getHibernateLazyInitializer().getIdentifier(), e) == null; } else { return map.put(e.getId(), e)==null; } }
Example #25
Source File: JavassistProxyFactory.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public HibernateProxy getProxy( Serializable id, SharedSessionContractImplementor session) throws HibernateException { final JavassistLazyInitializer initializer = new JavassistLazyInitializer( entityName, persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals ); try { final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance(); ( (Proxy) proxy ).setHandler( initializer ); initializer.constructed(); return proxy; } catch (Throwable t) { LOG.error( LOG.bytecodeEnhancementFailed( entityName ), t ); throw new HibernateException( LOG.bytecodeEnhancementFailed( entityName ), t ); } }
Example #26
Source File: SimpleHibernateProxyHandler.java From gorm-hibernate5 with Apache License 2.0 | 5 votes |
public boolean isInitialized(Object o) { if (o instanceof HibernateProxy) { return !((HibernateProxy) o).getHibernateLazyInitializer().isUninitialized(); } else if (o instanceof PersistentCollection) { return ((PersistentCollection) o).wasInitialized(); } else { return super.isInitialized(o); } }
Example #27
Source File: StatefulPersistenceContext.java From lams with GNU General Public License v2.0 | 5 votes |
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 ); }
Example #28
Source File: ForeignKeys.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * Is this instance persistent or detached? * If <tt>assumed</tt> is non-null, don't hit the database to make the * determination, instead assume that value; the client code must be * prepared to "recover" in the case that this assumed result is incorrect. */ public static boolean isNotTransient(String entityName, Object entity, Boolean assumed, SessionImplementor session) throws HibernateException { if (entity instanceof HibernateProxy) return true; if ( session.getPersistenceContext().isEntryFor(entity) ) return true; return !isTransient(entityName, entity, assumed, session); }
Example #29
Source File: SimpleHibernateProxyHandler.java From gorm-hibernate5 with Apache License 2.0 | 5 votes |
public Object unwrapProxy(final HibernateProxy proxy) { final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer(); if (lazyInitializer.isUninitialized()) { lazyInitializer.initialize(); } final Object obj = lazyInitializer.getImplementation(); if (obj != null) { ensureCorrectGroovyMetaClass(obj, obj.getClass()); } return obj; }
Example #30
Source File: Hibernate.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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; } }