tags:

views:

272

answers:

2

Hi guys,

Every so often, I get a "org.hibernate.QueryException: duplicate association path: myAssociation". This is because the complex criteria I'm working with can define the same path in many places. I'd love to do something like

Criteria association = myCriteria.getAssociation("wax");
if(association == null) association = myCriteria.createCriteria("wax");

Is there any such way that I can check if an association is already in place?

Cheers

Nik

+2  A: 

There's no way to do that via Criteria API. You can, however, maintain a Map yourself if you really need this to happen.

You can either:

  • have one map for each criteria instance, nested criteria keyed by alias.
  • have a single global map; its key would have to have both the alias of nested criteria and the path from root to current criteria.
ChssPly76
Yup, this is what I ended up doing, just keeping them around. Would be nice to be able to look them up, though would save me an awful lot of if-tests and puts. :-)
niklassaers
A: 

Actually you could find Subcriteria by alias, but the code is not published (i.e. in CriteriaImpl class). See my example below:

private Subcriteria getCritria(Criteria pCriteria, String pAlias) {
    Iterator<Subcriteria> iter = ((CriteriaImpl)pCriteria).iterateSubcriteria();
    while (iter.hasNext()){
        Subcriteria sub = iter.next();
        if (pAlias.equals(sub.getAlias())){
            return sub;
        }
    }
    return null;
}
FoxyBOA