List of usage examples for org.hibernate.proxy HibernateProxy writeReplace
public Object writeReplace();
From source file:org.ambraproject.rhino.util.JsonAdapterUtil.java
License:Open Source License
/** * If the object is a lazy-loaded HibernateProxy, force it to be replaced with its actual write-replacement. This can * be used to work around Hibernate optimizations that disrupt Gson's automatic serialization. * <p/>//from www . j a v a 2 s. co m * This may incur extra performance costs if the full object would not have otherwise been read. * * @param object a Hibernate model entity, which may be lazy-loaded * @param classToken the model type * @param <T> the model type * @return a non-lazy-loading instance of the entity * @see org.hibernate.proxy.HibernateProxy#writeReplace() */ public static <T> T forceHibernateLazyLoad(T object, Class<T> classToken) { Preconditions.checkNotNull(classToken); if (object instanceof HibernateProxy) { HibernateProxy hibernateProxy = (HibernateProxy) object; return classToken.cast(hibernateProxy.writeReplace()); } return object; }
From source file:org.granite.hibernate.jmf.EntityCodec.java
License:Open Source License
public void encode(ExtendedObjectOutput out, Object v) throws IOException, IllegalAccessException, InvocationTargetException { String detachedState = null;//from www .ja v a 2s . c o m if (v instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy) v; // Only write initialized flag, detachedState & id if v is an uninitialized proxy. if (proxy.getHibernateLazyInitializer().isUninitialized()) { Class<?> persistentClass = proxy.getHibernateLazyInitializer().getPersistentClass(); if (!serializableProxyAdapters.containsKey(persistentClass)) { try { SerializableProxyAdapter proxyAdapter = new SerializableProxyAdapter(proxy.writeReplace()); serializableProxyAdapters.putIfAbsent(persistentClass, proxyAdapter); } catch (Exception e) { throw new IOException("Could not create SerializableProxyAdapter for: " + proxy); } } Serializable id = proxy.getHibernateLazyInitializer().getIdentifier(); log.debug("Writing uninitialized HibernateProxy %s with id %s", detachedState, id); out.writeBoolean(false); out.writeUTF(null); out.writeObject(id); return; } // Proxy is initialized, get the underlying persistent object. log.debug("Writing initialized HibernateProxy..."); v = proxy.getHibernateLazyInitializer().getImplementation(); } // Write initialized flag & detachedState. out.writeBoolean(true); out.writeUTF(null); // Write all fields in lexical order. List<Property> fields = out.getReflection().findSerializableFields(v.getClass()); for (Property field : fields) out.getAndWriteField(v, field); }