Search in sources :

Example 1 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy 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 HibernateProxy

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

the class AuditedCollectionProxyTest method testProxyIdentifier.

@Test
public void testProxyIdentifier() {
    EntityManager em = getEntityManager();
    em.getTransaction().begin();
    ListRefEdEntity listReferencedEntity1 = em.getReference(ListRefEdEntity.class, id_ListRefEdEntity1);
    assert listReferencedEntity1 instanceof HibernateProxy;
    // Revision 3
    ListRefIngEntity refingEntity3 = new ListRefIngEntity(Integer.valueOf(3), "refing3", listReferencedEntity1);
    em.persist(refingEntity3);
    listReferencedEntity1.getReffering().size();
    em.getTransaction().commit();
}
Also used : EntityManager(javax.persistence.EntityManager) ListRefIngEntity(org.hibernate.envers.test.entities.onetomany.ListRefIngEntity) HibernateProxy(org.hibernate.proxy.HibernateProxy) ListRefEdEntity(org.hibernate.envers.test.entities.onetomany.ListRefEdEntity) Test(org.junit.Test)

Example 3 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy 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 4 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy 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 5 with HibernateProxy

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

the class LazyGroupAccessTestTask method execute.

@Override
public void execute() {
    Session s = getFactory().openSession();
    s.beginTransaction();
    Child c1 = (Child) s.createQuery("from Child c where c.name = :name").setString("name", "steve").uniqueResult();
    // verify the expected initial loaded state
    assertLoaded(c1, "name");
    assertNotLoaded(c1, "nickName");
    assertNotLoaded(c1, "parent");
    assertNotLoaded(c1, "alternateParent");
    // Now lets access nickName which ought to initialize nickName and parent, but not alternateParent
    c1.getNickName();
    assertLoaded(c1, "nickName");
    assertLoaded(c1, "parent");
    assertNotLoaded(c1, "alternateParent");
    assertEquals("Hibernate", c1.getParent().getNombre());
    assertFalse(c1.getParent() instanceof HibernateProxy);
    Child c2 = (Child) s.createQuery("from Child c where c.name = :name").setString("name", "sally").uniqueResult();
    // verify the expected initial loaded state
    assertLoaded(c2, "name");
    assertNotLoaded(c2, "nickName");
    assertNotLoaded(c2, "parent");
    assertNotLoaded(c2, "alternateParent");
    // Now lets access alternateParent which ought to initialize alternateParent and nothing else
    c2.getAlternateParent();
    assertNotLoaded(c2, "nickName");
    assertNotLoaded(c2, "parent");
    assertLoaded(c2, "alternateParent");
    assertEquals("Hibernate", c2.getAlternateParent().getNombre());
    assertFalse(c2.getAlternateParent() instanceof HibernateProxy);
    s.getTransaction().commit();
    s.close();
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)131 Session (org.hibernate.Session)58 Test (org.junit.Test)56 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 DefaultPostLoaderDao (org.broadleafcommerce.common.persistence.DefaultPostLoaderDao)16 PostLoaderDao (org.broadleafcommerce.common.persistence.PostLoaderDao)16 EntityEntry (org.hibernate.engine.spi.EntityEntry)13 Serializable (java.io.Serializable)11 Transaction (org.hibernate.Transaction)10 TransientObjectException (org.hibernate.TransientObjectException)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 HibernateException (org.hibernate.HibernateException)8 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)5 EntityKey (org.hibernate.engine.spi.EntityKey)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Iterator (java.util.Iterator)3 ObjectDeletedException (org.hibernate.ObjectDeletedException)3 EventSource (org.hibernate.event.spi.EventSource)3 BigDecimal (java.math.BigDecimal)2