sql的join语句转成hql出现的问题

来源:互联网 发布:windows dos 切换目录 编辑:程序博客网 时间:2024/06/16 07:32

我用sql语句写出了一条join语句

select distinct m.name from messagebox m inner join reply r on m.getperson_id = r.send_reply_person_id where r.send_reply_person_id=1 unionselect m.name from messagebox m where m.sendperson_id=1;


但是想写成hql的时候遇到了问题,

我试了很多hql语句,比如

//Query query = getSession().createQuery(" from Reply as o left join o.getPerson join ");

他总是生成诸如

SELECT * FROM 表A  LEFT JOIN 表B ON 表A.字段12(主键) = 表B.字段1(<set>中key的值)

比如

Hibernate: select messagebox0_.id as id1_0_, replies1_.id as id2_1_, messagebox0_.name as name1_0_, messagebox0_.content as content1_0_, messagebox0_.sendTime as sendTime1_0_, messagebox0_.sendPerson_id as sendPerson5_1_0_, messagebox0_.getPerson_id as getPerson6_1_0_, messagebox0_.collect as collect1_0_, messagebox0_.canBeReplied as canBeRep8_1_0_, messagebox0_.rubbish as rubbish1_0_, replies1_.title as title2_1_, replies1_.content as content2_1_, replies1_.PARENT_REPLY_ID as PARENT4_2_1_, replies1_.MESSAGEBOX_ID as MESSAGEBOX5_2_1_, replies1_.SEND_REPLY_PERSON_ID as SEND6_2_1_, replies1_.SEND_REPLY_TIME as SEND7_2_1_ from MessageBox messagebox0_ left outer join reply replies1_ on messagebox0_.id=replies1_.MESSAGEBOX_ID

我的hbm.xml

<class name="MessageBox" table="MessageBox"><id name="id"  type="string" column="id"><generator class="uuid"></generator></id>  <set name="replies" lazy="false"  inverse="true">        <key column="MESSAGEBOX_ID"/>        <one-to-many class="Reply"/>     </set>

 

我需要改成SELECT * FROM 表A LEFT JOIN 表B ON 表A.字段12(非主键,且任意由自己定) = 表B.字段1(任意)

于是我在网上查看,有个兄弟遇到了同样的问题,但也没用解决好。他的问题:http://bbs.csdn.net/topics/260028474

所以改用了sqlQuery

原创粉丝点击