Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Criteria by associationPath

Tags:

java

hibernate

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

like image 281
niklassaers Avatar asked Nov 27 '09 14:11

niklassaers


1 Answers

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;
}
like image 103
FoxyBOA Avatar answered Oct 20 '22 20:10

FoxyBOA