@Override public NaturalIdLoadAccess byNaturalId(String entityName) { return session.byNaturalId( entityName ); }
@Override public NaturalIdLoadAccess byNaturalId(Class entityClass) { return session.byNaturalId( entityClass ); }
@Override public NaturalIdLoadAccess byNaturalId(String entityName) { return new NaturalIdLoadAccessImpl( entityName ); }
@Override public NaturalIdLoadAccess byNaturalId(Class entityClass) { return new NaturalIdLoadAccessImpl( entityClass ); }
@Override public List list(Criteria criteria) throws HibernateException { // TODO: Is this guaranteed to always be CriteriaImpl? CriteriaImpl criteriaImpl = (CriteriaImpl) criteria; final NaturalIdLoadAccess naturalIdLoadAccess = this.tryNaturalIdLoadAccess( criteriaImpl ); if ( naturalIdLoadAccess != null ) { // EARLY EXIT! return Arrays.asList( naturalIdLoadAccess.load() ); } errorIfClosed(); checkTransactionSynchStatus(); String[] implementors = factory.getImplementors( criteriaImpl.getEntityOrClassName() ); int size = implementors.length; CriteriaLoader[] loaders = new CriteriaLoader[size]; Set spaces = new HashSet(); for( int i=0; i <size; i++ ) { loaders[i] = new CriteriaLoader( getOuterJoinLoadable( implementors[i] ), factory, criteriaImpl, implementors[i], getLoadQueryInfluencers() ); spaces.addAll( loaders[i].getQuerySpaces() ); } autoFlushIfRequired(spaces); List results = Collections.EMPTY_LIST; dontFlushFromFind++; boolean success = false; try { for( int i=0; i<size; i++ ) { final List currentResults = loaders[i].list(this); currentResults.addAll(results); results = currentResults; } success = true; } finally { dontFlushFromFind--; afterOperation(success); delayedAfterCompletion(); } return results; }
/** * 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 = factory.getEntityPersister( entityName ); // Verify the entity actually has a natural id, needed for legacy support as NaturalIdentifier criteria // 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 = (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 i = 0; i < naturalIdentifierProperties.length; i++ ) { final String naturalIdProperty = propertyNames[naturalIdentifierProperties[i]]; 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 ); } // Critera 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; }
@Override public NaturalIdLoadAccess using(String attributeName, Object value) { naturalIdParameters.put( attributeName, value ); return this; }
protected <P> NaturalIdLoadAccess byNaturalId(Class<P> entityClass) { return this.getSession().byNaturalId(entityClass); }
protected <P> NaturalIdLoadAccess byNaturalId(String entityName) { return this.getSession().byNaturalId(entityName); }