Search in sources :

Example 6 with LazyInitializer

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

the class ReadOnlyProxyTest method testModifiableViaLazyInitializerAfterInit.

@Test
public void testModifiableViaLazyInitializerAfterInit() {
    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);
    dp.setDescription("changed");
    assertFalse(dpLI.isUninitialized());
    assertEquals("changed", dp.getDescription());
    checkReadOnly(s, dp, false);
    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("changed", 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)

Example 7 with LazyInitializer

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

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerDoesNotInit.

@Test
public void testReadOnlyViaLazyInitializerDoesNotInit() {
    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);
    assertFalse(Hibernate.isInitialized(dp));
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    assertFalse(Hibernate.isInitialized(dp));
    dpLI.setReadOnly(false);
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    s.flush();
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    s.getTransaction().commit();
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    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)

Example 8 with LazyInitializer

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

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerAfterInit.

@Test
public void testReadOnlyViaLazyInitializerAfterInit() {
    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);
    dp.setDescription("changed");
    assertFalse(dpLI.isUninitialized());
    assertEquals("changed", dp.getDescription());
    checkReadOnly(s, dp, false);
    dpLI.setReadOnly(true);
    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();
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with LazyInitializer

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

the class StatefulPersistenceContext method reassociateIfUninitializedProxy.

@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
    if (!Hibernate.isInitialized(value)) {
        final HibernateProxy proxy = (HibernateProxy) value;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        reassociateProxy(li, proxy);
        return true;
    } else {
        return false;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 10 with LazyInitializer

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

the class SessionImpl method bestGuessEntityName.

@Override
public String bestGuessEntityName(Object object) {
    if (object instanceof HibernateProxy) {
        LazyInitializer initializer = ((HibernateProxy) object).getHibernateLazyInitializer();
        // so make certain that we do not accidentally initialize an uninitialized proxy
        if (initializer.isUninitialized()) {
            return initializer.getEntityName();
        }
        object = initializer.getImplementation();
    }
    EntityEntry entry = persistenceContext.getEntry(object);
    if (entry == null) {
        return guessEntityName(object);
    } else {
        return entry.getPersister().getEntityName();
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

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