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