use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class DefaultMergeEventListener method onMerge.
/**
* Handle the given merge event.
*
* @param event The merge event to be handled.
*
* @throws HibernateException
*/
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
final MergeContext copyCache = (MergeContext) copiedAlready;
final EventSource source = event.getSession();
final Object original = event.getOriginal();
if (original != null) {
final Object entity;
if (original instanceof HibernateProxy) {
LazyInitializer li = ((HibernateProxy) original).getHibernateLazyInitializer();
if (li.isUninitialized()) {
LOG.trace("Ignoring uninitialized proxy");
event.setResult(source.load(li.getEntityName(), li.getIdentifier()));
// EARLY EXIT!
return;
} else {
entity = li.getImplementation();
}
} else {
entity = original;
}
if (copyCache.containsKey(entity) && (copyCache.isOperatedOn(entity))) {
LOG.trace("Already in merge process");
event.setResult(entity);
} else {
if (copyCache.containsKey(entity)) {
LOG.trace("Already in copyCache; setting in merge process");
copyCache.setOperatedOn(entity, true);
}
event.setEntity(entity);
EntityState entityState = null;
// Check the persistence context for an entry relating to this
// entity to be merged...
EntityEntry entry = source.getPersistenceContext().getEntry(entity);
if (entry == null) {
EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
Serializable id = persister.getIdentifier(entity, source);
if (id != null) {
final EntityKey key = source.generateEntityKey(id, persister);
final Object managedEntity = source.getPersistenceContext().getEntity(key);
entry = source.getPersistenceContext().getEntry(managedEntity);
if (entry != null) {
// we have specialized case of a detached entity from the
// perspective of the merge operation. Specifically, we
// have an incoming entity instance which has a corresponding
// entry in the current persistence context, but registered
// under a different entity instance
entityState = EntityState.DETACHED;
}
}
}
if (entityState == null) {
entityState = getEntityState(entity, event.getEntityName(), entry, source);
}
switch(entityState) {
case DETACHED:
entityIsDetached(event, copyCache);
break;
case TRANSIENT:
entityIsTransient(event, copyCache);
break;
case PERSISTENT:
entityIsPersistent(event, copyCache);
break;
default:
// DELETED
throw new ObjectDeletedException("deleted instance passed to merge", null, getLoggableName(event.getEntityName(), entity));
}
}
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class DefaultEvictEventListener method onEvict.
/**
* Handle the given evict event.
*
* @param event The evict event to be handled.
*
* @throws HibernateException
*/
public void onEvict(EvictEvent event) throws HibernateException {
final Object object = event.getObject();
if (object == null) {
throw new NullPointerException("null passed to Session.evict()");
}
final EventSource source = event.getSession();
final PersistenceContext persistenceContext = source.getPersistenceContext();
if (object instanceof HibernateProxy) {
final LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
final Serializable id = li.getIdentifier();
if (id == null) {
throw new IllegalArgumentException("Could not determine identifier of proxy passed to evict()");
}
final EntityPersister persister = source.getFactory().getEntityPersister(li.getEntityName());
final EntityKey key = source.generateEntityKey(id, persister);
persistenceContext.removeProxy(key);
if (!li.isUninitialized()) {
final Object entity = persistenceContext.removeEntity(key);
if (entity != null) {
EntityEntry e = persistenceContext.removeEntry(entity);
doEvict(entity, key, e.getPersister(), event.getSession());
}
}
li.unsetSession();
} else {
EntityEntry e = persistenceContext.getEntry(object);
if (e != null) {
doEvict(object, e.getEntityKey(), e.getPersister(), source);
} else {
// see if the passed object is even an entity, and if not throw an exception
// this is different than legacy Hibernate behavior, but what JPA 2.1 is calling for
// with EntityManager.detach
EntityPersister persister = null;
final String entityName = persistenceContext.getSession().guessEntityName(object);
if (entityName != null) {
try {
persister = persistenceContext.getSession().getFactory().getEntityPersister(entityName);
} catch (Exception ignore) {
}
}
if (persister == null) {
throw new IllegalArgumentException("Non-entity object instance passed to evict : " + object);
}
}
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class AnyType method guessEntityPersister.
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);
}
use of org.hibernate.proxy.LazyInitializer in project ma-core-public by infiniteautomation.
the class H3PropertyDescriptorProperty method getValue.
/* (non-Javadoc)
* @see org.directwebremoting.impl.PropertyDescriptorProperty#getValue(java.lang.Object)
*/
public Object getValue(Object bean) throws MarshallException {
if (!(bean instanceof HibernateProxy)) {
// This is not a hibernate dynamic proxy, just use it
return super.getValue(bean);
} else {
// If the property is already initialized, use it
boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
if (initialized) {
// This might be a lazy-collection so we need to double check
Object reply = super.getValue(bean);
initialized = Hibernate.isInitialized(reply);
}
if (initialized) {
return super.getValue(bean);
} else {
// If the session bound to the property is live, use it
HibernateProxy proxy = (HibernateProxy) bean;
LazyInitializer initializer = proxy.getHibernateLazyInitializer();
SessionImplementor implementor = initializer.getSession();
if (implementor.isOpen()) {
return super.getValue(bean);
}
// So the property needs database access, and the session is closed
// We'll need to try get another session
ServletContext context = WebContextFactory.get().getServletContext();
Session session = H3SessionAjaxFilter.getCurrentSession(context);
if (session != null) {
session.update(bean);
return super.getValue(bean);
}
return null;
}
}
}
use of org.hibernate.proxy.LazyInitializer in project BroadleafCommerce by BroadleafCommerce.
the class HibernateUtils method deproxy.
/**
* <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;
}
Aggregations