use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class CollectionType method getIdOfOwnerOrNull.
/**
* Get the id value from the owning entity key, usually the same as the key, but might be some
* other property, in the case of property-ref
*
* @param key The collection owner key
* @param session The session from which the request is originating.
* @return The collection owner's id, if it can be obtained from the key;
* otherwise, null is returned
*/
public Serializable getIdOfOwnerOrNull(Serializable key, SharedSessionContractImplementor session) {
Serializable ownerId = null;
if (foreignKeyPropertyName == null) {
ownerId = key;
} else {
Type keyType = getPersister(session).getKeyType();
EntityPersister ownerPersister = getPersister(session).getOwnerEntityPersister();
// TODO: Fix this so it will work for non-POJO entity mode
Class ownerMappedClass = ownerPersister.getMappedClass();
if (ownerMappedClass.isAssignableFrom(keyType.getReturnedClass()) && keyType.getReturnedClass().isInstance(key)) {
// the key is the owning entity itself, so get the ID from the key
ownerId = ownerPersister.getIdentifier(key, session);
} else {
// TODO: check if key contains the owner ID
}
}
return ownerId;
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class CompositeBasedAssociationAttribute method determineFetchPlan.
@Override
public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
final EntityPersister owningPersister = getSource().locateOwningPersister();
FetchStyle style = FetchStrategyHelper.determineFetchStyleByProfile(loadQueryInfluencers, owningPersister, propertyPath, attributeNumber());
if (style == null) {
style = determineFetchStyleByMetadata(getFetchMode(), getType());
}
return new FetchStrategy(determineFetchTiming(style), style);
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class AbstractEntityTuplizer method determineEntityIdPersistIfNecessary.
private static Serializable determineEntityIdPersistIfNecessary(Object entity, AssociationType associationType, SharedSessionContractImplementor session, SessionFactoryImplementor sessionFactory) {
if (entity == null) {
return null;
}
if (HibernateProxy.class.isInstance(entity)) {
// entity is a proxy, so we know it is not transient; just return ID from proxy
return ((HibernateProxy) entity).getHibernateLazyInitializer().getIdentifier();
}
if (session != null) {
final EntityEntry pcEntry = session.getPersistenceContext().getEntry(entity);
if (pcEntry != null) {
// entity managed; return ID.
return pcEntry.getId();
}
}
final EntityPersister persister = resolveEntityPersister(entity, associationType, session, sessionFactory);
Serializable entityId = persister.getIdentifier(entity, session);
if (entityId == null) {
if (session != null) {
// if we have a session, then follow the HHH-11328 requirements
entityId = persistTransientEntity(entity, session);
}
// otherwise just let it be null HHH-11274
} else {
if (session != null) {
// if the entity is in the process of being merged, it may be stored in the
// PC already, but doesn't have an EntityEntry yet. If this is the case,
// then don't persist even if it is transient because doing so can lead
// to having 2 entities in the PC with the same ID (HHH-11328).
final EntityKey entityKey = session.generateEntityKey(entityId, persister);
if (session.getPersistenceContext().getEntity(entityKey) == null && ForeignKeys.isTransient(persister.getEntityName(), entity, null, session)) {
// entity is transient and it is not in the PersistenceContext.
// entity needs to be persisted.
persistTransientEntity(entity, session);
}
}
}
return entityId;
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class CacheKeySerializationTest method testId.
private void testId(CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception {
final SessionFactoryImplementor sessionFactory = getSessionFactory(cacheKeysFactory.getClass().getName());
final EntityPersister persister = sessionFactory.getEntityPersister(entityName);
final Object key = cacheKeysFactory.createEntityKey(id, persister, sessionFactory, null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(key);
final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
final Object keyClone = ois.readObject();
try {
assertEquals(key, keyClone);
assertEquals(keyClone, key);
assertEquals(key.hashCode(), keyClone.hashCode());
final Object idClone = cacheKeysFactory.getEntityId(keyClone);
assertEquals(id.hashCode(), idClone.hashCode());
assertEquals(id, idClone);
assertEquals(idClone, id);
assertTrue(persister.getIdentifierType().isEqual(id, idClone, sessionFactory));
assertTrue(persister.getIdentifierType().isEqual(idClone, id, sessionFactory));
sessionFactory.close();
} finally {
sessionFactory.close();
}
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class MonotonicRevisionNumberTest method testOracleSequenceOrder.
@Test
public void testOracleSequenceOrder() {
EntityPersister persister = sessionFactory().getEntityPersister(SequenceIdRevisionEntity.class.getName());
IdentifierGenerator generator = persister.getIdentifierGenerator();
Assert.assertTrue(OrderedSequenceGenerator.class.isInstance(generator));
OrderedSequenceGenerator seqGenerator = (OrderedSequenceGenerator) generator;
Assert.assertTrue("Oracle sequence needs to be ordered in RAC environment.", seqGenerator.sqlCreateStrings(getDialect())[0].toLowerCase().endsWith(" order"));
}
Aggregations