在hibernate的hql查询中,假如说分组信息与试题是多对多关系,那么我们要在hql查询中对含有多个分组信息的试题都要查询出来。并同时查询出相应试题的分组信息。那么此时hql要这样写:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 += ") " ; |
附上多对多配置:
引用
1
2
3
4
5
6
7
8
|
< 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 > |
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://wjch-111.iteye.com/blog/1973031