在
hibernate的hql查询中,假如说分组信息与试题是多对多关系,那么我们要在hql查询中对含有多个分组信息的试题都要查询出来。并同时查询出相应试题的分组信息。那么此时hql要这样写:
class="java" name="code">
String[] groupIds = ojbects[1].toString().split(",");
String hql = "SELECT distinct a.id FROM TmEduExamContent a";
if(!"".equals(groupIds[0])){
hql += " join a.groups as b ";
}
hql += "and ( ";
for(int i=0;i<groupIds.length;i++){
hql += "b.id="+groupIds[i] + " ";
if(i != groupIds.length-1){
hql += "or ";
}
}
hql += ") ";
附上多对多配置:
引用
<set name="groups" table="TM_EDUCATION_CONTENT_GROUP" outer-join="false">
<key column="CONTENT_ID" />
<
many-to-many class="com.gd.po.ClassicCasesGroup" column="GROUP_ID" />
</set>
<set name="modelContents" table="TM_MODELCONTENT_GROUP" outer-join="false">
<key column="GROUP_ID" />
<many-to-many class="com.gd.po.TmEduExamModelContent" column="MODELCONTENT_ID" />
</set>