Hibernate的一對多查詢及去掉重複的物件distinct
阿新 • • 發佈:2019-02-03
問:sql 中 select * from A left join B on A.id=B.id where A.id=? 如果在Hibernate 中 用HQL 怎麼表達呢 ?
答:from A left join A.b b where A.id=?
其中b是在實體類A的hibernate配置檔案中對映的物件B
<hibernate-mapping package="com.XXXX.entities">
<class name="A" table="a_table">
<set name="b" lazy="false" inverse="true" cascade="all">
<key column="a_table_id" />
<one-to-many class="com.XXXX.entities.B" />
</set>
</class>
</hibernate-mapping>
注意:條件連線是 where 而不是on
如果要查詢不重複的,select DISTINCT(A) from A left join A.b where A.id=?
答:from A left join A.b b where A.id=?
其中b是在實體類A的hibernate配置檔案中對映的物件B
<hibernate-mapping package="com.XXXX.entities">
<class name="A" table="a_table">
<set name="b" lazy="false" inverse="true" cascade="all">
<key column="a_table_id" />
<one-to-many class="com.XXXX.entities.B" />
</set>
</class>
</hibernate-mapping>
注意:條件連線是 where 而不是on
如果要查詢不重複的,select DISTINCT(A) from A left join A.b where A.id=?