use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class SessionImpl method getIdentifier.
// not for internal use:
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
checkOpen();
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();
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ByteBuddyProxyFactory method getProxy.
@Override
public HibernateProxy getProxy(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
final ByteBuddyInterceptor interceptor = new ByteBuddyInterceptor(entityName, persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals);
try {
final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
((ProxyConfiguration) proxy).$$_hibernate_set_interceptor(interceptor);
return proxy;
} catch (Throwable t) {
LOG.error(LOG.bytecodeEnhancementFailed(entityName), t);
throw new HibernateException(LOG.bytecodeEnhancementFailed(entityName), t);
}
}
use of org.hibernate.proxy.HibernateProxy in project hibernate-orm by hibernate.
the class ByteBuddyProxyFactory method deserializeProxy.
public static 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);
}
}
use of org.hibernate.proxy.HibernateProxy in project webofneeds by researchstudio-sat.
the class ParentAwareFlushEventListener method onFlushEntity.
@Override
public void onFlushEntity(final FlushEntityEvent event) throws HibernateException {
final EntityEntry entry = event.getEntityEntry();
final Object entity = event.getEntity();
final boolean mightBeDirty = entry.requiresDirtyCheck(entity);
if (mightBeDirty && entity instanceof ParentAware) {
ParentAware parentAware = (ParentAware) entity;
if (updated(event)) {
VersionedEntity parent = parentAware.getParent();
if (parent == null)
return;
if (logger.isDebugEnabled()) {
logger.debug("Incrementing {} entity version because a {} child entity has been updated", parent, entity);
}
if (!(parent instanceof HibernateProxy)) {
// we have to do the increment manually
parent.incrementVersion();
}
Hibernate.initialize(parent);
event.getSession().save(parent);
} else if (deleted(event)) {
VersionedEntity parent = parentAware.getParent();
if (parent == null)
return;
if (logger.isDebugEnabled()) {
logger.debug("Incrementing {} entity version because a {} child entity has been deleted", root, entity);
}
if (!(parent instanceof HibernateProxy)) {
// we have to do the increment manually
parent.incrementVersion();
}
Hibernate.initialize(parent);
event.getSession().save(parent);
}
}
}
use of org.hibernate.proxy.HibernateProxy in project CzechIdMng by bcvsolutions.
the class DefaultAuditService method getValuesFromVersion.
@Override
public Map<String, Object> getValuesFromVersion(Object revisionObject, List<String> auditedClass) {
Map<String, Object> revisionValues = new HashMap<>();
if (revisionObject == null) {
return Collections.emptyMap();
}
// for better debug and readable there is no stream
// we cannot use Introspector.getBeanInfo
// getAllFieldsList get all field also with field from superclass
List<Field> fields = FieldUtils.getAllFieldsList(revisionObject.getClass());
for (Field field : fields) {
try {
// check if field has Audited annotation, or class
if (!field.isAnnotationPresent(Audited.class) && !field.getDeclaringClass().isAnnotationPresent(Audited.class)) {
continue;
}
//
PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(revisionObject, field.getName());
// get property descriptor for readMethod
if (propertyDescriptor == null) {
continue;
}
//
Method readMethod = propertyDescriptor.getReadMethod();
Object value = readMethod.invoke(revisionObject);
// value can be null, but we want it
if (value == null) {
revisionValues.put(field.getName(), null);
continue;
}
//
LazyInitializer hibernateLI = null;
String className = null;
if (value instanceof HibernateProxy) {
HibernateProxy proxy = (HibernateProxy) value;
hibernateLI = proxy.getHibernateLazyInitializer();
className = hibernateLI.getEntityName();
}
// we have all audited class, then some not audited class (binding) and others primitive types
if (className != null) {
// get id from hibernate lazy initializer, entity may no longer exist, but ID in DB is always
revisionValues.put(field.getName(), hibernateLI.getIdentifier());
} else {
revisionValues.put(field.getName(), value);
}
//
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new ResultCodeException(CoreResultCode.BAD_REQUEST, ImmutableMap.of("field", field.getName()), e);
}
}
return revisionValues;
}
Aggregations