Search in sources :

Example 66 with EntityPersister

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

the class DefaultResolveNaturalIdEventListener method resolveNaturalId.

/**
	 * Coordinates the efforts to load a given entity. First, an attempt is
	 * made to load the entity from the session-level cache. If not found there,
	 * an attempt is made to locate it in second-level cache. Lastly, an
	 * attempt is made to load it directly from the datasource.
	 * 
	 * @param event The load event
	 *
	 * @return The loaded entity, or null.
	 */
protected Serializable resolveNaturalId(final ResolveNaturalIdEvent event) {
    final EntityPersister persister = event.getEntityPersister();
    final boolean traceEnabled = LOG.isTraceEnabled();
    if (traceEnabled) {
        LOG.tracev("Attempting to resolve: {0}", MessageHelper.infoString(persister, event.getNaturalIdValues(), event.getSession().getFactory()));
    }
    Serializable entityId = resolveFromCache(event);
    if (entityId != null) {
        if (traceEnabled) {
            LOG.tracev("Resolved object in cache: {0}", MessageHelper.infoString(persister, event.getNaturalIdValues(), event.getSession().getFactory()));
        }
        return entityId;
    }
    if (traceEnabled) {
        LOG.tracev("Object not resolved in any cache: {0}", MessageHelper.infoString(persister, event.getNaturalIdValues(), event.getSession().getFactory()));
    }
    return loadFromDatasource(event);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable)

Example 67 with EntityPersister

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

the class FromElementType method applyTreatAsDeclarations.

public void applyTreatAsDeclarations(Set<String> treatAsDeclarations) {
    if (treatAsDeclarations != null && !treatAsDeclarations.isEmpty()) {
        if (this.treatAsDeclarations == null) {
            this.treatAsDeclarations = new HashSet<String>();
        }
        for (String treatAsSubclassName : treatAsDeclarations) {
            try {
                EntityPersister subclassPersister = fromElement.getSessionFactoryHelper().requireClassPersister(treatAsSubclassName);
                this.treatAsDeclarations.add(subclassPersister.getEntityName());
            } catch (SemanticException e) {
                throw new QueryException("Unable to locate persister for subclass named in TREAT-AS : " + treatAsSubclassName);
            }
        }
        if (joinSequence != null) {
            joinSequence.applyTreatAsDeclarations(this.treatAsDeclarations);
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) QueryException(org.hibernate.QueryException) SemanticException(antlr.SemanticException)

Example 68 with EntityPersister

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

the class DotNode method isReferenceToPrimaryKey.

/**
	 * Is the given property name a reference to the primary key of the associated
	 * entity construed by the given entity type?
	 * <p/>
	 * For example, consider a fragment like order.customer.id
	 * (where order is a from-element alias).  Here, we'd have:
	 * propertyName = "id" AND
	 * owningType = ManyToOneType(Customer)
	 * and are being asked to determine whether "customer.id" is a reference
	 * to customer's PK...
	 *
	 * @param propertyName The name of the property to check.
	 * @param owningType The type represeting the entity "owning" the property
	 *
	 * @return True if propertyName references the entity's (owningType->associatedEntity)
	 *         primary key; false otherwise.
	 */
private boolean isReferenceToPrimaryKey(String propertyName, EntityType owningType) {
    EntityPersister persister = getSessionFactoryHelper().getFactory().getEntityPersister(owningType.getAssociatedEntityName());
    if (persister.getEntityMetamodel().hasNonIdentifierPropertyNamedId()) {
        // only the identifier property field name can be a reference to the associated entity's PK...
        return propertyName.equals(persister.getIdentifierPropertyName()) && owningType.isReferenceToPrimaryKey();
    }
    // the referenced node text is the special 'id'
    if (EntityPersister.ENTITY_ID.equals(propertyName)) {
        return owningType.isReferenceToPrimaryKey();
    }
    String keyPropertyName = getSessionFactoryHelper().getIdentifierOrUniqueKeyPropertyName(owningType);
    return keyPropertyName != null && keyPropertyName.equals(propertyName) && owningType.isReferenceToPrimaryKey();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister)

Example 69 with EntityPersister

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

the class MapKeyEntityFromElement method buildKeyJoin.

public static MapKeyEntityFromElement buildKeyJoin(FromElement collectionFromElement) {
    final HqlSqlWalker walker = collectionFromElement.getWalker();
    final SessionFactoryHelper sfh = walker.getSessionFactoryHelper();
    final SessionFactoryImplementor sf = sfh.getFactory();
    final QueryableCollection collectionPersister = collectionFromElement.getQueryableCollection();
    final Type indexType = collectionPersister.getIndexType();
    if (indexType == null) {
        throw new IllegalArgumentException("Given collection is not indexed");
    }
    if (!indexType.isEntityType()) {
        throw new IllegalArgumentException("Given collection does not have an entity index");
    }
    final EntityType indexEntityType = (EntityType) indexType;
    final EntityPersister indexEntityPersister = (EntityPersister) indexEntityType.getAssociatedJoinable(sf);
    final String rhsAlias = walker.getAliasGenerator().createName(indexEntityPersister.getEntityName());
    final boolean useThetaJoin = collectionFromElement.getJoinSequence().isThetaStyle();
    MapKeyEntityFromElement join = new MapKeyEntityFromElement(useThetaJoin);
    join.initialize(HqlSqlTokenTypes.JOIN_FRAGMENT, ((Joinable) indexEntityPersister).getTableName());
    join.initialize(collectionFromElement.getWalker());
    join.initializeEntity(collectionFromElement.getFromClause(), indexEntityPersister.getEntityName(), indexEntityPersister, indexEntityType, "<map-key-join-" + collectionFromElement.getClassAlias() + ">", rhsAlias);
    //		String[] joinColumns = determineJoinColuns( collectionPersister, joinTableAlias );
    // todo : assumes columns, no formulas
    String[] joinColumns = collectionPersister.getIndexColumnNames(collectionFromElement.getCollectionTableAlias());
    JoinSequence joinSequence = sfh.createJoinSequence(useThetaJoin, indexEntityType, rhsAlias, //				JoinType.INNER_JOIN,
    collectionFromElement.getJoinSequence().getFirstJoin().getJoinType(), joinColumns);
    join.setJoinSequence(joinSequence);
    join.setOrigin(collectionFromElement, true);
    join.setColumns(joinColumns);
    join.setUseFromFragment(collectionFromElement.useFromFragment());
    join.setUseWhereFragment(collectionFromElement.useWhereFragment());
    walker.addQuerySpaces(indexEntityPersister.getQuerySpaces());
    return join;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SessionFactoryHelper(org.hibernate.hql.internal.ast.util.SessionFactoryHelper) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) HqlSqlWalker(org.hibernate.hql.internal.ast.HqlSqlWalker) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 70 with EntityPersister

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

the class CacheImpl method containsEntity.

@Override
public boolean containsEntity(String entityName, Serializable identifier) {
    EntityPersister p = sessionFactory.getMetamodel().entityPersister(entityName);
    if (p.hasCache()) {
        EntityRegionAccessStrategy cache = p.getCacheAccessStrategy();
        // have to assume non tenancy
        Object key = cache.generateCacheKey(identifier, p, sessionFactory, null);
        return cache.getRegion().contains(key);
    } else {
        return false;
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy)

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