JSP developed hibernate hql query many to many query

  • 2020-10-31 21:44:29
  • OfStack

JSP developed hibernate's hql query many-to-many query

In hql query of hibernate, if the grouping information is many-to-many relation with the test questions, then we should search out the questions with more than one grouping information in hql query. And at the same time query the corresponding questions grouping information. So hql would write this:


 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 += ") "; 

Attached is a many-to-many configuration:

reference


  <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> 

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: