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);
}
}
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);
}
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());
}
Aggregations