use of org.hibernate.proxy.LazyInitializer 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;
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerBeforeInit.
@Test
public void testReadOnlyViaLazyInitializerBeforeInit() {
DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
s.beginTransaction();
DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
assertTrue(dp instanceof HibernateProxy);
LazyInitializer dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
assertTrue(dpLI.isUninitialized());
checkReadOnly(s, dp, false);
dpLI.setReadOnly(true);
checkReadOnly(s, dp, true);
dp.setDescription("changed");
assertFalse(dpLI.isUninitialized());
assertEquals("changed", dp.getDescription());
checkReadOnly(s, dp, true);
s.flush();
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
dp = (DataPoint) s.get(DataPoint.class, dpOrig.getId());
assertEquals(dpOrig.getId(), dp.getId());
assertEquals(dpOrig.getDescription(), dp.getDescription());
assertEquals(dpOrig.getX(), dp.getX());
assertEquals(dpOrig.getY(), dp.getY());
s.delete(dp);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method reassociateProxy.
@Override
public void reassociateProxy(Object value, Serializable id) throws MappingException {
if (value instanceof HibernateProxy) {
LOG.debugf("Setting proxy identifier: %s", id);
final HibernateProxy proxy = (HibernateProxy) value;
final LazyInitializer li = proxy.getHibernateLazyInitializer();
li.setIdentifier(id);
reassociateProxy(li, proxy);
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method narrowProxy.
@Override
@SuppressWarnings("unchecked")
public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object) throws HibernateException {
final Class concreteProxyClass = persister.getConcreteProxyClass();
final boolean alreadyNarrow = concreteProxyClass.isInstance(proxy);
if (!alreadyNarrow) {
LOG.narrowingProxy(concreteProxyClass);
// It would just be extra processing. Just return the impl
if (object != null) {
proxiesByKey.remove(key);
return object;
}
// Similarly, if the original HibernateProxy is initialized, there
// is again no point in creating a proxy. Just return the impl
final HibernateProxy originalHibernateProxy = (HibernateProxy) proxy;
if (!originalHibernateProxy.getHibernateLazyInitializer().isUninitialized()) {
final Object impl = originalHibernateProxy.getHibernateLazyInitializer().getImplementation();
// can we return it?
if (concreteProxyClass.isInstance(impl)) {
proxiesByKey.remove(key);
return impl;
}
}
// Otherwise, create the narrowed proxy
final HibernateProxy narrowedProxy = (HibernateProxy) persister.createProxy(key.getIdentifier(), session);
// set the read-only/modifiable mode in the new proxy to what it was in the original proxy
final boolean readOnlyOrig = originalHibernateProxy.getHibernateLazyInitializer().isReadOnly();
narrowedProxy.getHibernateLazyInitializer().setReadOnly(readOnlyOrig);
return narrowedProxy;
} else {
if (object != null) {
final LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
li.setImplementation(object);
}
return proxy;
}
}
use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.
the class ReadOnlyCriteriaQueryTest method checkProxyReadOnly.
private void checkProxyReadOnly(Session s, Object proxy, boolean expectedReadOnly) {
assertTrue(proxy instanceof HibernateProxy);
LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
assertSame(s, li.getSession());
assertEquals(expectedReadOnly, s.isReadOnly(proxy));
assertEquals(expectedReadOnly, li.isReadOnly());
assertEquals(Hibernate.isInitialized(proxy), !li.isUninitialized());
if (Hibernate.isInitialized(proxy)) {
assertEquals(expectedReadOnly, s.isReadOnly(li.getImplementation()));
}
}
Aggregations