Search in sources :

Example 1 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class EntityInstantiator method replaceNonAuditIdProxies.

@SuppressWarnings({ "unchecked" })
private void replaceNonAuditIdProxies(Map versionsEntity, Number revision) {
    final Map originalId = (Map) versionsEntity.get(enversService.getAuditEntitiesConfiguration().getOriginalIdPropName());
    for (Object key : originalId.keySet()) {
        final Object value = originalId.get(key);
        if (value instanceof HibernateProxy) {
            final HibernateProxy hibernateProxy = (HibernateProxy) value;
            final LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
            final String entityName = initializer.getEntityName();
            final Serializable entityId = initializer.getIdentifier();
            if (enversService.getEntitiesConfigurations().isVersioned(entityName)) {
                final String entityClassName = enversService.getEntitiesConfigurations().get(entityName).getEntityClassName();
                final Class entityClass = ReflectionTools.loadClass(entityClassName, enversService.getClassLoaderService());
                final ToOneDelegateSessionImplementor delegate = new ToOneDelegateSessionImplementor(versionsReader, entityClass, entityId, revision, RevisionType.DEL.equals(versionsEntity.get(enversService.getAuditEntitiesConfiguration().getRevisionTypePropName())), enversService);
                originalId.put(key, versionsReader.getSessionImplementor().getFactory().getMetamodel().entityPersister(entityName).createProxy(entityId, delegate));
            }
        }
    }
}
Also used : ToOneDelegateSessionImplementor(org.hibernate.envers.internal.entities.mapper.relation.lazy.ToOneDelegateSessionImplementor) LazyInitializer(org.hibernate.proxy.LazyInitializer) Serializable(java.io.Serializable) Map(java.util.Map) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 2 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class ProxyIdentifier method testProxyIdentifier.

@Test
public void testProxyIdentifier() {
    TargetNotAuditedEntity rev1 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1.getId(), 1);
    Assert.assertTrue(rev1.getReference() instanceof HibernateProxy);
    HibernateProxy proxyCreateByEnvers = (HibernateProxy) rev1.getReference();
    LazyInitializer lazyInitializer = proxyCreateByEnvers.getHibernateLazyInitializer();
    Assert.assertTrue(lazyInitializer.isUninitialized());
    Assert.assertNotNull(lazyInitializer.getIdentifier());
    Assert.assertEquals(tnae1.getId(), lazyInitializer.getIdentifier());
    Assert.assertTrue(lazyInitializer.isUninitialized());
    Assert.assertEquals(uste1.getId(), rev1.getReference().getId());
    Assert.assertEquals(uste1.getStr(), rev1.getReference().getStr());
    Assert.assertFalse(lazyInitializer.isUninitialized());
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) TargetNotAuditedEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity) Test(org.junit.Test)

Example 3 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project head by mifos.

the class InterceptHelper method getClazz.

private Class getClazz(Object obj) {
    try {
        HibernateProxy hibernateProxy = (HibernateProxy) obj;
        LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
        return lazyInitializer.getPersistentClass();
    } catch (ClassCastException e) {
        return obj.getClass();
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 4 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class ReadOnlyProxyTest method checkReadOnly.

private void checkReadOnly(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()));
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 5 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerNoChangeAfterInit.

@Test
public void testReadOnlyViaLazyInitializerNoChangeAfterInit() {
    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();
    checkReadOnly(s, dp, false);
    assertTrue(dpLI.isUninitialized());
    Hibernate.initialize(dp);
    assertFalse(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dp instanceof HibernateProxy);
    dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    assertTrue(dpLI.isUninitialized());
    Hibernate.initialize(dp);
    assertFalse(dpLI.isUninitialized());
    checkReadOnly(s, dp, true);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dp instanceof HibernateProxy);
    dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    assertTrue(dpLI.isUninitialized());
    dpLI.setReadOnly(false);
    checkReadOnly(s, dp, false);
    assertTrue(dpLI.isUninitialized());
    Hibernate.initialize(dp);
    assertFalse(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    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();
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)33 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 Session (org.hibernate.Session)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Test (org.junit.Test)7 Serializable (java.io.Serializable)3 HibernateException (org.hibernate.HibernateException)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 MappingException (org.hibernate.MappingException)2 ObjectDeletedException (org.hibernate.ObjectDeletedException)2 PersistentObjectException (org.hibernate.PersistentObjectException)2 SessionImplementor (org.hibernate.engine.SessionImplementor)2 EntityKey (org.hibernate.engine.spi.EntityKey)2 EventSource (org.hibernate.event.spi.EventSource)2 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1