简体   繁体   中英

Hibernate criteria alias generates innej join which eats query results

I have a DAO that searches objects using criteria.list() that searches Accidents. It should return accidents which:

1) Have type A and accidentAttribs (mapped as collection but there is only one element inside) TITLE = Test

2) All other accidents which are not of type A, even if they doesnt have any attribs

Creating this alias:

 criteria.createAlias("accidentAttribsSet", "accidentAttrib");

makes inner join between two tables and accidents without attributes are not returned..

criteria.createAlias("accidentAttribsSet", "accidentAttrib");
                    criteria = criteria.add(Restrictions.or(

                                    Restrictions.not(Restrictions.eq("type", "A")),
                                    Restrictions.and( Restrictions.eq("type", "A"),Restrictions.eq("accidentAttrib.title", "Test") )
                                    )

                    );

I db I have accidents of type B and they dont have any attributes yet and this query doesnt return them.

An inner join means that only accidents with attributes will be in the result set. You should try to use a left join

criteria.createAlias("accidentAttribsSet", "accidentAttrib", JoinType.LEFT.ordinal());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM