我有2个表,note表和tag表,是一对多的关系,即每一个note都有若干个tag与之对应,tag表的外键是note.Id,两个表的mapping文件和实体类如下所示:
note表<hibernate-mapping>
<class name="entity.Note" table="note">
<id name="id" column="Id" >
<generator class="identity"/>
</id>
<property name="noteText" column="noteText" type="string"/>
<property name="endTime" column="endTime" type="date"/>
<set name="tags" cascade="all-delete-orphan" inverse="true">
<key column="noteId"/>
<one-to-many class="entity.Tag"/>
</set>
</class>
</hibernate-mapping>
Note的实体类private int id;
private String noteText;
private Date endTime;
private Set<Tag> tags = new HashSet<Tag>();
tag表<hibernate-mapping>
<class name="entity.Tag" table="tag">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="content" column="content" type="string"/>
<many-to-one name="note" class="entity.Note" column="noteId"/>
</class>
</hibernate-mapping>
Tag的实体类private int id;
private String content;
private Note note;
我想写一条HQL语句,可以实现如下的功能:查找出能够匹配若干个tag.content值的note.noteText的值。(如果将note理解为某个记录对象,tag则为记录的关键词,我想实现的功能就是根据若干的关键词查找出能够匹配的记录对象)。希望高手解答!

解决方案 »

  1.   

    因为lz发错版了哦
    建议发到
    java技术->Web开发版
      

  2.   

    from Note note where note.id in(select tag.note.id from Tag tag where tag.content in('关键值1','关键值2'..))
    希望没错。 
      

  3.   

    请问:你的这条语句和from Note as n left join fetch n.tags nt where nt.content in ('关键值1','关键值2'..))   有没有什么区别啊 ?特别是他们返回的note对象中,note.tags有没有什么区别啊???
      

  4.   

    有区别应该, 我那个只是为了获取Note的查询, 而且是inner join
    你的那个是left join fetch 可能会把note.tag的内容也获取了。 
      

  5.   

    你具体看日志吧, hibernate的SQL日志具体生成什么