com.anasoft.os.daofusion.criteria
Class AssociationPathRegister

java.lang.Object
  extended by com.anasoft.os.daofusion.criteria.AssociationPathRegister

public class AssociationPathRegister
extends java.lang.Object

Register of AssociationPath instances and corresponding Hibernate Criteria to be reused when modifying the root Criteria instance.

This class is used by NestedPropertyCriteria to initialize CriteriaImpl.Subcriteria mappings in a safe way, avoiding the duplicate association path Hibernate Criteria API issue.

AssociationPathRegister is a thin wrapper around the given Criteria instance that gets initialized with existing CriteriaImpl.Subcriteria mappings at construction time. It is therefore safe to create multiple AssociationPathRegister instances operating on the same Criteria.

You can use this class to modify Criteria instances in a safe way on your own as well (always prefer get(AssociationPath) in favor of calling the createCriteria method directly).

Author:
michal.jemala, vojtech.szocs
See Also:
AssociationPath, NestedPropertyCriteria

Constructor Summary
AssociationPathRegister(org.hibernate.Criteria rootCriteria)
          Creates a new association path register, rooted at the given Criteria instance.
 
Method Summary
 org.hibernate.Criteria get(AssociationPath associationPath)
          Returns a Criteria instance for the given AssociationPath.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AssociationPathRegister

public AssociationPathRegister(org.hibernate.Criteria rootCriteria)
Creates a new association path register, rooted at the given Criteria instance.

Note that the register is initialized with Hibernate Criteria mappings according to the current state of rootCriteria.

Parameters:
rootCriteria - Root Criteria instance.
Method Detail

get

public org.hibernate.Criteria get(AssociationPath associationPath)
Returns a Criteria instance for the given AssociationPath.

This method ensures that Hibernate Criteria mappings are lazily initialized (with existing criteria instances being reused) prior to returning the target Criteria.

Resulting Criteria instances have unique aliases (based on their association paths) so that they can be referenced in complex Hibernate queries like this:

 Criteria criteria1 = apRegister.get(associationPath1);
 String alias1 = criteria1.getAlias();
 
 Criteria criteria2 = apRegister.get(associationPath2);
 String alias2 = criteria2.getAlias();
 
 rootCriteria.add(
     Restrictions.or(
         Restrictions.eq(alias1 + "." + targetPropertyName1, value1),
         Restrictions.eq(alias2 + "." + targetPropertyName2, value2)
     ));
 
You can safely call this method multiple times with same association path argument. Note that unused association path criteria instances might break your query behavior when using certain join types (e.g. inner join).

Parameters:
associationPath - Association path for which to obtain the Criteria instance.
Returns:
Criteria instance for the given AssociationPath.


Copyright © 2008-2009 ANASOFT and contributors. All Rights Reserved.