Search in sources :

Example 86 with EntityPersister

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;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable)

Example 87 with EntityPersister

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);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) FetchStyle(org.hibernate.engine.FetchStyle) FetchStrategy(org.hibernate.engine.FetchStrategy)

Example 88 with EntityPersister

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;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable)

Example 89 with EntityPersister

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();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 90 with EntityPersister

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"));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SequenceIdRevisionEntity(org.hibernate.envers.enhanced.SequenceIdRevisionEntity) OrderedSequenceGenerator(org.hibernate.envers.enhanced.OrderedSequenceGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Aggregations

EntityPersister (org.hibernate.persister.entity.EntityPersister)166 Test (org.junit.Test)59 Serializable (java.io.Serializable)34 Session (org.hibernate.Session)31 Type (org.hibernate.type.Type)29 EntityEntry (org.hibernate.engine.spi.EntityEntry)21 EventSource (org.hibernate.event.spi.EventSource)15 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)13 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 TestForIssue (org.hibernate.testing.TestForIssue)13 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)12 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)12 EntityType (org.hibernate.type.EntityType)12 ArrayList (java.util.ArrayList)11 CompositeType (org.hibernate.type.CompositeType)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 EntityKey (org.hibernate.engine.spi.EntityKey)8 QueryParameters (org.hibernate.engine.spi.QueryParameters)8