Search in sources :

Example 1 with NaturalIdentifier

use of org.hibernate.criterion.NaturalIdentifier 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;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) SimpleNaturalIdLoadAccess(org.hibernate.SimpleNaturalIdLoadAccess) CriterionEntry(org.hibernate.internal.CriteriaImpl.CriterionEntry) NaturalIdentifier(org.hibernate.criterion.NaturalIdentifier)

Aggregations

NaturalIdLoadAccess (org.hibernate.NaturalIdLoadAccess)1 SimpleNaturalIdLoadAccess (org.hibernate.SimpleNaturalIdLoadAccess)1 NaturalIdentifier (org.hibernate.criterion.NaturalIdentifier)1 CriterionEntry (org.hibernate.internal.CriteriaImpl.CriterionEntry)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1