use of org.hibernate.proxy.HibernateProxy 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.HibernateProxy in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method onSaveOrUpdate.
/**
* Handle the given update event.
*
* @param event The update event to be handled.
*/
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
final SessionImplementor source = event.getSession();
final Object object = event.getObject();
final Serializable requestedId = event.getRequestedId();
if (requestedId != null) {
// reassociating the proxy
if (object instanceof HibernateProxy) {
((HibernateProxy) object).getHibernateLazyInitializer().setIdentifier(requestedId);
}
}
// For an uninitialized proxy, noop, don't even need to return an id, since it is never a save()
if (reassociateIfUninitializedProxy(object, source)) {
LOG.trace("Reassociated uninitialized proxy");
} else {
// initialize properties of the event:
final Object entity = source.getPersistenceContext().unproxyAndReassociate(object);
event.setEntity(entity);
event.setEntry(source.getPersistenceContext().getEntry(entity));
// return the id in the event object
event.setResultId(performSaveOrUpdate(event));
}
}
use of org.hibernate.proxy.HibernateProxy 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.HibernateProxy in project hibernate-orm by hibernate.
the class ProxyTest method testProxySerialization.
@Test
public void testProxySerialization() {
Session s = openSession();
Transaction t = s.beginTransaction();
DataPoint dp = new DataPoint();
dp.setDescription("a data point");
dp.setX(new BigDecimal(1.0));
dp.setY(new BigDecimal(2.0));
s.persist(dp);
s.flush();
s.clear();
dp = (DataPoint) s.load(DataPoint.class, new Long(dp.getId()));
assertFalse(Hibernate.isInitialized(dp));
dp.getId();
assertFalse(Hibernate.isInitialized(dp));
dp.getDescription();
assertTrue(Hibernate.isInitialized(dp));
Object none = s.load(DataPoint.class, new Long(666));
assertFalse(Hibernate.isInitialized(none));
t.commit();
s.disconnect();
Object[] holder = new Object[] { s, dp, none };
holder = (Object[]) SerializationHelper.clone(holder);
Session sclone = (Session) holder[0];
dp = (DataPoint) holder[1];
none = holder[2];
// close the original:
s.close();
t = sclone.beginTransaction();
DataPoint sdp = (DataPoint) sclone.load(DataPoint.class, new Long(dp.getId()));
assertSame(dp, sdp);
assertFalse(sdp instanceof HibernateProxy);
Object snone = sclone.load(DataPoint.class, new Long(666));
assertSame(none, snone);
assertTrue(snone instanceof HibernateProxy);
sclone.delete(dp);
t.commit();
sclone.close();
}
use of org.hibernate.proxy.HibernateProxy 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;
}
}
}
Aggregations