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.resolveFactory().iterateEntityNameResolvers()) { entityName = resolver.resolveEntityName(entity); if (entityName != null) { break; } } } if (entityName == null) { // the old-time stand-by... entityName = object.getClass().getName(); } return scope.resolveFactory().getEntityPersister(entityName); }
/** Associate a proxy that was instantiated by another session with this session */ private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) throws HibernateException { if (li.getSession() != this) { EntityPersister persister = session.getFactory().getEntityPersister(li.getEntityName()); EntityKey key = new EntityKey(li.getIdentifier(), persister, session.getEntityMode()); if (!proxiesByKey.containsKey(key)) proxiesByKey.put(key, proxy); // any earlier proxy takes precedence proxy.getHibernateLazyInitializer().setSession(session); } }
/** * 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; } }
protected Class<?> getClass(ExtendedObjectOutput out, Object v) { Class<?> cls = v.getClass(); if (v instanceof HibernateProxy) { LazyInitializer initializer = ((HibernateProxy) v).getHibernateLazyInitializer(); String className = (initializer.isUninitialized() ? initializer.getEntityName() : initializer.getImplementation().getClass().getName()); if (className != null && className.length() > 0) { try { cls = out.getReflection().loadClass(className); } catch (ClassNotFoundException e) { cls = initializer.getPersistentClass(); } } } return cls; }
/** * @param proxy * @return * @throws ClassNotFoundException */ public static Object loadLazyObject(HibernateProxy proxy) throws ClassNotFoundException { LazyInitializer init = proxy.getHibernateLazyInitializer(); logger.debug(init.getEntityName() + StringUtils.SPACE + init.getIdentifier()); return HibernateUtil.getObject(Class.forName(init.getEntityName()), init.getIdentifier()); }