Search in sources :

Example 6 with UniqueKeyLoadable

use of org.hibernate.persister.entity.UniqueKeyLoadable in project hibernate-orm by hibernate.

the class NoProxyFetchStrategyHelperTest method determineAssociationType.

private AssociationType determineAssociationType(Class<?> entityClass, String path) {
    OuterJoinLoadable entityPersister = (OuterJoinLoadable) sessionFactory().getEntityPersister(entityClass.getName());
    int index = ((UniqueKeyLoadable) entityPersister).getPropertyIndex(path);
    return (AssociationType) entityPersister.getSubclassPropertyType(index);
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable)

Example 7 with UniqueKeyLoadable

use of org.hibernate.persister.entity.UniqueKeyLoadable in project hibernate-orm by hibernate.

the class Loader method loadFromResultSet.

/**
 * Hydrate the state an object from the SQL <tt>ResultSet</tt>, into
 * an array or "hydrated" values (do not resolve associations yet),
 * and pass the hydrates state to the session.
 */
private void loadFromResultSet(final ResultSet rs, final int i, final Object object, final String instanceEntityName, final EntityKey key, final String rowIdAlias, final LockMode lockMode, final Loadable rootPersister, final SharedSessionContractImplementor session) throws SQLException, HibernateException {
    final Serializable id = key.getIdentifier();
    // Get the persister for the _subclass_
    final Loadable persister = (Loadable) getFactory().getEntityPersister(instanceEntityName);
    if (LOG.isTraceEnabled()) {
        LOG.tracef("Initializing object from ResultSet: %s", MessageHelper.infoString(persister, id, getFactory()));
    }
    boolean fetchAllPropertiesRequested = isEagerPropertyFetchEnabled(i);
    // add temp entry so that the next step is circular-reference
    // safe - only needed because some types don't take proper
    // advantage of two-phase-load (esp. components)
    TwoPhaseLoad.addUninitializedEntity(key, object, persister, lockMode, session);
    // This is not very nice (and quite slow):
    final String[][] cols = persister == rootPersister ? getEntityAliases()[i].getSuffixedPropertyAliases() : getEntityAliases()[i].getSuffixedPropertyAliases(persister);
    final Object[] values = persister.hydrate(rs, id, object, rootPersister, cols, fetchAllPropertiesRequested, session);
    final Object rowId = persister.hasRowId() ? rs.getObject(rowIdAlias) : null;
    final AssociationType[] ownerAssociationTypes = getOwnerAssociationTypes();
    if (ownerAssociationTypes != null && ownerAssociationTypes[i] != null) {
        String ukName = ownerAssociationTypes[i].getRHSUniqueKeyPropertyName();
        if (ukName != null) {
            final int index = ((UniqueKeyLoadable) persister).getPropertyIndex(ukName);
            final Type type = persister.getPropertyTypes()[index];
            // polymorphism not really handled completely correctly,
            // perhaps...well, actually its ok, assuming that the
            // entity name used in the lookup is the same as the
            // the one used here, which it will be
            EntityUniqueKey euk = new EntityUniqueKey(// polymorphism comment above
            rootPersister.getEntityName(), ukName, type.semiResolve(values[index], session, object), type, persister.getEntityMode(), session.getFactory());
            session.getPersistenceContext().addEntity(euk, object);
        }
    }
    TwoPhaseLoad.postHydrate(persister, id, values, rowId, object, lockMode, session);
}
Also used : UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) Serializable(java.io.Serializable) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey)

Example 8 with UniqueKeyLoadable

use of org.hibernate.persister.entity.UniqueKeyLoadable in project hibernate-orm by hibernate.

the class EntityType method loadByUniqueKey.

/**
 * Load an instance by a unique key that is not the primary key.
 *
 * @param entityName The name of the entity to load
 * @param uniqueKeyPropertyName The name of the property defining the uniqie key.
 * @param key The unique key property value.
 * @param session The originating session.
 *
 * @return The loaded entity
 *
 * @throws HibernateException generally indicates problems performing the load.
 */
public Object loadByUniqueKey(String entityName, String uniqueKeyPropertyName, Object key, SharedSessionContractImplementor session) throws HibernateException {
    final SessionFactoryImplementor factory = session.getFactory();
    UniqueKeyLoadable persister = (UniqueKeyLoadable) factory.getMetamodel().entityPersister(entityName);
    // TODO: implement 2nd level caching?! natural id caching ?! proxies?!
    EntityUniqueKey euk = new EntityUniqueKey(entityName, uniqueKeyPropertyName, key, getIdentifierOrUniqueKeyType(factory), persister.getEntityMode(), session.getFactory());
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    Object result = persistenceContext.getEntity(euk);
    if (result == null) {
        result = persister.loadByUniqueKey(uniqueKeyPropertyName, key, session);
        // add it to the Persistence Context
        if (result != null) {
            persistenceContext.addEntity(euk, result);
        }
    }
    return result == null ? null : persistenceContext.proxyFor(result);
}
Also used : EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable)

Example 9 with UniqueKeyLoadable

use of org.hibernate.persister.entity.UniqueKeyLoadable in project hibernate-orm by hibernate.

the class EntityReferenceInitializerImpl method loadFromResultSet.

private void loadFromResultSet(ResultSet resultSet, ResultSetProcessingContext context, Object entityInstance, String concreteEntityTypeName, EntityKey entityKey, LockMode lockModeToAcquire) {
    final Serializable id = entityKey.getIdentifier();
    // Get the persister for the _subclass_
    final Loadable concreteEntityPersister = (Loadable) context.getSession().getFactory().getMetamodel().entityPersister(concreteEntityTypeName);
    if (log.isTraceEnabled()) {
        log.tracev("Initializing object from ResultSet: {0}", MessageHelper.infoString(concreteEntityPersister, id, context.getSession().getFactory()));
    }
    // add temp entry so that the next step is circular-reference
    // safe - only needed because some types don't take proper
    // advantage of two-phase-load (esp. components)
    TwoPhaseLoad.addUninitializedEntity(entityKey, entityInstance, concreteEntityPersister, lockModeToAcquire, context.getSession());
    final EntityPersister rootEntityPersister = context.getSession().getFactory().getMetamodel().entityPersister(concreteEntityPersister.getRootEntityName());
    final Object[] values;
    try {
        values = concreteEntityPersister.hydrate(resultSet, id, entityInstance, (Loadable) entityReference.getEntityPersister(), concreteEntityPersister == rootEntityPersister ? entityReferenceAliases.getColumnAliases().getSuffixedPropertyAliases() : entityReferenceAliases.getColumnAliases().getSuffixedPropertyAliases(concreteEntityPersister), context.getLoadPlan().areLazyAttributesForceFetched(), context.getSession());
        context.getProcessingState(entityReference).registerHydratedState(values);
    } catch (SQLException e) {
        throw context.getSession().getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlExceptionHelper().convert(e, "Could not read entity state from ResultSet : " + entityKey);
    }
    final Object rowId;
    try {
        rowId = concreteEntityPersister.hasRowId() ? resultSet.getObject(entityReferenceAliases.getColumnAliases().getRowIdAlias()) : null;
        if (rowId != null && log.isTraceEnabled()) {
            log.tracev("extracted ROWID value: {0}", rowId);
        }
    } catch (SQLException e) {
        throw context.getSession().getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlExceptionHelper().convert(e, "Could not read entity row-id from ResultSet : " + entityKey);
    }
    final EntityType entityType = EntityFetch.class.isInstance(entityReference) ? ((EntityFetch) entityReference).getFetchedType() : entityReference.getEntityPersister().getEntityMetamodel().getEntityType();
    if (entityType != null) {
        String ukName = entityType.getRHSUniqueKeyPropertyName();
        if (ukName != null) {
            final int index = ((UniqueKeyLoadable) concreteEntityPersister).getPropertyIndex(ukName);
            final Type type = concreteEntityPersister.getPropertyTypes()[index];
            // polymorphism not really handled completely correctly,
            // perhaps...well, actually its ok, assuming that the
            // entity name used in the lookup is the same as the
            // the one used here, which it will be
            EntityUniqueKey euk = new EntityUniqueKey(entityReference.getEntityPersister().getEntityName(), ukName, type.semiResolve(values[index], context.getSession(), entityInstance), type, concreteEntityPersister.getEntityMode(), context.getSession().getFactory());
            context.getSession().getPersistenceContext().addEntity(euk, entityInstance);
        }
    }
    TwoPhaseLoad.postHydrate(concreteEntityPersister, id, values, rowId, entityInstance, lockModeToAcquire, context.getSession());
    context.registerHydratedEntity(entityReference, entityKey, entityInstance);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) EntityType(org.hibernate.type.EntityType) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) Type(org.hibernate.type.Type) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey)

Aggregations

UniqueKeyLoadable (org.hibernate.persister.entity.UniqueKeyLoadable)9 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)6 AssociationType (org.hibernate.type.AssociationType)4 EntityUniqueKey (org.hibernate.engine.spi.EntityUniqueKey)3 Serializable (java.io.Serializable)2 Loadable (org.hibernate.persister.entity.Loadable)2 EntityType (org.hibernate.type.EntityType)2 Type (org.hibernate.type.Type)2 VersionType (org.hibernate.type.VersionType)2 SQLException (java.sql.SQLException)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1