Search in sources :

Example 6 with PersistentAttributeInterceptable

use of org.hibernate.engine.spi.PersistentAttributeInterceptable in project hibernate-orm by hibernate.

the class AbstractEntityPersister method initializeLazyPropertiesFromDatastore.

private Object initializeLazyPropertiesFromDatastore(final String fieldName, final Object entity, final SharedSessionContractImplementor session, final Serializable id, final EntityEntry entry) {
    if (!hasLazyProperties()) {
        throw new AssertionFailure("no lazy properties");
    }
    final InterceptorImplementor interceptor = ((PersistentAttributeInterceptable) entity).$$_hibernate_getInterceptor();
    assert interceptor != null : "Expecting bytecode interceptor to be non-null";
    LOG.trace("Initializing lazy properties from datastore");
    final String fetchGroup = getEntityMetamodel().getBytecodeEnhancementMetadata().getLazyAttributesMetadata().getFetchGroupName(fieldName);
    final List<LazyAttributeDescriptor> fetchGroupAttributeDescriptors = getEntityMetamodel().getBytecodeEnhancementMetadata().getLazyAttributesMetadata().getFetchGroupAttributeDescriptors(fetchGroup);
    final Set<String> initializedLazyAttributeNames = interceptor.getInitializedLazyAttributeNames();
    final String lazySelect = getSQLLazySelectString(fetchGroup);
    try {
        Object result = null;
        PreparedStatement ps = null;
        try {
            ResultSet rs = null;
            try {
                if (lazySelect != null) {
                    // null sql means that the only lazy properties
                    // are shared PK one-to-one associations which are
                    // handled differently in the Type#nullSafeGet code...
                    ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(lazySelect);
                    getIdentifierType().nullSafeSet(ps, id, 1, session);
                    rs = session.getJdbcCoordinator().getResultSetReturn().extract(ps);
                    rs.next();
                }
                final Object[] snapshot = entry.getLoadedState();
                for (LazyAttributeDescriptor fetchGroupAttributeDescriptor : fetchGroupAttributeDescriptors) {
                    final boolean previousInitialized = initializedLazyAttributeNames.contains(fetchGroupAttributeDescriptor.getName());
                    if (previousInitialized) {
                        // its already been initialized (e.g. by a write) so we don't want to overwrite
                        continue;
                    }
                    final Object selectedValue = fetchGroupAttributeDescriptor.getType().nullSafeGet(rs, lazyPropertyColumnAliases[fetchGroupAttributeDescriptor.getLazyIndex()], session, entity);
                    final boolean set = initializeLazyProperty(fieldName, entity, session, snapshot, fetchGroupAttributeDescriptor.getLazyIndex(), selectedValue);
                    if (set) {
                        result = selectedValue;
                        interceptor.attributeInitialized(fetchGroupAttributeDescriptor.getName());
                    }
                }
            } finally {
                if (rs != null) {
                    session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, ps);
                }
            }
        } finally {
            if (ps != null) {
                session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(ps);
                session.getJdbcCoordinator().afterStatementExecution();
            }
        }
        LOG.trace("Done initializing lazy properties");
        return result;
    } catch (SQLException sqle) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not initialize lazy properties: " + MessageHelper.infoString(this, id, getFactory()), lazySelect);
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SQLException(java.sql.SQLException) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) LazyAttributeDescriptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor)

Example 7 with PersistentAttributeInterceptable

use of org.hibernate.engine.spi.PersistentAttributeInterceptable in project hibernate-orm by hibernate.

the class BasicEnhancementTestTask method execute.

public void execute() {
    SimpleEntity entity = new SimpleEntity();
    // Call the new ManagedEntity methods
    assertTyping(ManagedEntity.class, entity);
    ManagedEntity managedEntity = (ManagedEntity) entity;
    assertSame(entity, managedEntity.$$_hibernate_getEntityInstance());
    assertNull(managedEntity.$$_hibernate_getEntityEntry());
    managedEntity.$$_hibernate_setEntityEntry(EnhancerTestUtils.makeEntityEntry());
    assertNotNull(managedEntity.$$_hibernate_getEntityEntry());
    managedEntity.$$_hibernate_setEntityEntry(null);
    assertNull(managedEntity.$$_hibernate_getEntityEntry());
    managedEntity.$$_hibernate_setNextManagedEntity(managedEntity);
    managedEntity.$$_hibernate_setPreviousManagedEntity(managedEntity);
    assertSame(managedEntity, managedEntity.$$_hibernate_getNextManagedEntity());
    assertSame(managedEntity, managedEntity.$$_hibernate_getPreviousManagedEntity());
    // Add an attribute interceptor...
    assertTyping(PersistentAttributeInterceptable.class, entity);
    PersistentAttributeInterceptable interceptableEntity = (PersistentAttributeInterceptable) entity;
    assertNull(interceptableEntity.$$_hibernate_getInterceptor());
    interceptableEntity.$$_hibernate_setInterceptor(new ObjectAttributeMarkerInterceptor());
    assertNotNull(interceptableEntity.$$_hibernate_getInterceptor());
    assertNull(EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject"));
    entity.setAnObject(new Object());
    assertSame(EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject"), ObjectAttributeMarkerInterceptor.WRITE_MARKER);
    assertSame(entity.getAnObject(), ObjectAttributeMarkerInterceptor.READ_MARKER);
    entity.setAnObject(null);
    assertSame(EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject"), ObjectAttributeMarkerInterceptor.WRITE_MARKER);
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable)

Example 8 with PersistentAttributeInterceptable

use of org.hibernate.engine.spi.PersistentAttributeInterceptable in project hibernate-orm by hibernate.

the class ExtendedEnhancementTestTask method execute.

public void execute() {
    // test uses ObjectAttributeMarkerInterceptor to ensure that field access is routed through enhanced methods
    SimpleEntity entity = new SimpleEntity();
    ((PersistentAttributeInterceptable) entity).$$_hibernate_setInterceptor(new ObjectAttributeMarkerInterceptor());
    Object decoy = new Object();
    entity.anUnspecifiedObject = decoy;
    Object gotByReflection = EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject");
    Assert.assertNotSame(gotByReflection, decoy);
    Assert.assertSame(gotByReflection, ObjectAttributeMarkerInterceptor.WRITE_MARKER);
    Object entityObject = entity.anUnspecifiedObject;
    Assert.assertNotSame(entityObject, decoy);
    Assert.assertSame(entityObject, ObjectAttributeMarkerInterceptor.READ_MARKER);
    // do some more calls on the various types, without the interceptor
    ((PersistentAttributeInterceptable) entity).$$_hibernate_setInterceptor(null);
    entity.id = 1234567890l;
    Assert.assertEquals(entity.id, 1234567890l);
    entity.name = "Entity Name";
    Assert.assertSame(entity.name, "Entity Name");
    entity.active = true;
    Assert.assertTrue(entity.active);
    entity.someStrings = Arrays.asList("A", "B", "C", "D");
    Assert.assertArrayEquals(new String[] { "A", "B", "C", "D" }, entity.someStrings.toArray());
}
Also used : ObjectAttributeMarkerInterceptor(org.hibernate.test.bytecode.enhancement.basic.ObjectAttributeMarkerInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable)

Aggregations

PersistentAttributeInterceptable (org.hibernate.engine.spi.PersistentAttributeInterceptable)8 LazyAttributeLoadingInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor)4 AssertionFailure (org.hibernate.AssertionFailure)2 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)2 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 PersistentAttributeInterceptor (org.hibernate.engine.spi.PersistentAttributeInterceptor)2 Serializable (java.io.Serializable)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 LoadState (javax.persistence.spi.LoadState)1 HibernateException (org.hibernate.HibernateException)1 LazyAttributeDescriptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor)1 NotInstrumentedException (org.hibernate.bytecode.spi.NotInstrumentedException)1 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)1 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)1