use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class SessionImpl method tryNaturalIdLoadAccess.
/**
* Checks to see if the CriteriaImpl is a naturalId lookup that can be done via
* NaturalIdLoadAccess
*
* @param criteria The criteria to check as a complete natural identifier lookup.
*
* @return A fully configured NaturalIdLoadAccess or null, if null is returned the standard CriteriaImpl execution
* should be performed
*/
private NaturalIdLoadAccess tryNaturalIdLoadAccess(CriteriaImpl criteria) {
// See if the criteria lookup is by naturalId
if (!criteria.isLookupByNaturalKey()) {
return null;
}
final String entityName = criteria.getEntityOrClassName();
final EntityPersister entityPersister = getFactory().getMetamodel().entityPersister(entityName);
// queries did no natural id validation
if (!entityPersister.hasNaturalIdentifier()) {
return null;
}
// Since isLookupByNaturalKey is true there can be only one CriterionEntry and getCriterion() will
// return an instanceof NaturalIdentifier
final CriterionEntry criterionEntry = criteria.iterateExpressionEntries().next();
final NaturalIdentifier naturalIdentifier = (NaturalIdentifier) criterionEntry.getCriterion();
final Map<String, Object> naturalIdValues = naturalIdentifier.getNaturalIdValues();
final int[] naturalIdentifierProperties = entityPersister.getNaturalIdentifierProperties();
// Verify the NaturalIdentifier criterion includes all naturalId properties, first check that the property counts match
if (naturalIdentifierProperties.length != naturalIdValues.size()) {
return null;
}
final String[] propertyNames = entityPersister.getPropertyNames();
final NaturalIdLoadAccess naturalIdLoader = this.byNaturalId(entityName);
// Build NaturalIdLoadAccess and in the process verify all naturalId properties were specified
for (int naturalIdentifierProperty : naturalIdentifierProperties) {
final String naturalIdProperty = propertyNames[naturalIdentifierProperty];
final Object naturalIdValue = naturalIdValues.get(naturalIdProperty);
if (naturalIdValue == null) {
// A NaturalId property is missing from the critera query, can't use NaturalIdLoadAccess
return null;
}
naturalIdLoader.using(naturalIdProperty, naturalIdValue);
}
// Criteria query contains a valid naturalId, use the new API
log.warn("Session.byNaturalId(" + entityName + ") should be used for naturalId queries instead of Restrictions.naturalId() from a Criteria");
return naturalIdLoader;
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class JoinWalker method isJoinedFetchEnabledInMapping.
/**
* Does the mapping, and Hibernate default semantics, specify that
* this association should be fetched by outer joining
*/
protected boolean isJoinedFetchEnabledInMapping(FetchMode config, AssociationType type) throws MappingException {
if (!type.isEntityType() && !type.isCollectionType()) {
return false;
} else {
if (config == FetchMode.JOIN) {
return true;
}
if (config == FetchMode.SELECT) {
return false;
}
if (type.isEntityType()) {
//TODO: look at the owning property and check that it
// isn't lazy (by instrumentation)
EntityType entityType = (EntityType) type;
EntityPersister persister = getFactory().getEntityPersister(entityType.getAssociatedEntityName());
return !persister.hasProxy();
} else {
return false;
}
}
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class JpaDeleteEventListener method performDetachedEntityDeletionCheck.
@Override
protected void performDetachedEntityDeletionCheck(DeleteEvent event) {
EventSource source = event.getSession();
String entityName = event.getEntityName();
EntityPersister persister = source.getEntityPersister(entityName, event.getObject());
Serializable id = persister.getIdentifier(event.getObject(), source);
entityName = entityName == null ? source.guessEntityName(event.getObject()) : entityName;
throw new IllegalArgumentException("Removing a detached instance " + entityName + "#" + id);
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class AttributeNodeImpl method internalMakeKeySubgraph.
@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeKeySubgraph(Class<X> type) {
if (!attribute.isCollection()) {
throw new IllegalArgumentException(String.format("Non-collection attribute [%s] cannot be target of key subgraph", getAttributeName()));
}
final PluralAttributeImpl pluralAttribute = (PluralAttributeImpl) attribute;
if (pluralAttribute.getCollectionType() != PluralAttribute.CollectionType.MAP) {
throw new IllegalArgumentException(String.format("Non-Map attribute [%s] cannot be target of key subgraph", getAttributeName()));
}
final AssociationType attributeType = (AssociationType) Helper.resolveType(sessionFactory(), attribute);
final QueryableCollection collectionPersister = (QueryableCollection) attributeType.getAssociatedJoinable(sessionFactory());
final Type indexType = collectionPersister.getIndexType();
if (!indexType.isAssociationType()) {
throw new IllegalArgumentException(String.format("Map index [%s] is not an entity; cannot be target of key subgraph", getAttributeName()));
}
if (keySubgraphMap == null) {
keySubgraphMap = new HashMap<>();
}
final AssociationType indexAssociationType = (AssociationType) indexType;
final EntityPersister indexEntityPersister = (EntityPersister) indexAssociationType.getAssociatedJoinable(sessionFactory());
if (type == null) {
type = indexEntityPersister.getMappedClass();
} else {
if (!isTreatableAs(indexEntityPersister, type)) {
throw new IllegalArgumentException(String.format("Map key [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), indexEntityPersister.getMappedClass().getName()));
}
}
final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, attribute.getDeclaringType(), type);
keySubgraphMap.put(type, subgraph);
return subgraph;
}
use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.
the class PersistenceUnitUtilImpl method getIdentifierFromPersister.
private Object getIdentifierFromPersister(Object entity) {
Class<?> entityClass = Hibernate.getClass(entity);
EntityPersister persister = sessionFactory.getMetamodel().entityPersister(entityClass);
if (persister == null) {
throw new IllegalArgumentException(entityClass.getName() + " is not an entity");
}
return persister.getIdentifier(entity, null);
}
Aggregations