簡體   English   中英

HIbernate多對一聯接條件查詢

[英]HIbernate many to one join criteria query

我有帶有key_column作為主鍵的table1。 表2具有來自表1的key_column作為外鍵。 table2與table1有多對一的關系。

我想運行一個聯接查詢。

Select table1.*, table2.* from table1, table2 where table2.id = ?
    table2.some_column_other_than_key_column = ? 
    and table1.key_column = table2.key_column

<class name="tbl2class" table="tbl2" lazy="false">
   <many-to-one name="tbl1class" column="key_column"
        class="tbl1Class"
        cascade="none" lazy="false" fetch="join" update="false" insert="true" />

</class>

List<tbl2Class> tbl2List= getSession().createCriteria(tbl2Class.class)
            .add(Restrictions.eq("id", id))
            .add(Restrictions.eq("tbl1.someColumnOtherThanKeyColumn", messageType))
            .add(Restrictions.or(categoryRestriction, strategyRestriction))
            .list();

我收到一個異常提及問題e無法解決tbl1.someColumnOtherThanKeyColumn-原因-我做錯了什么。

public class Tbl1Class 
{
    private Tbl2Class tbl2Class
}

你應該用這樣的東西

Criteria tbl2Criteria = getSession().createCriteria(tbl2Class.class);
tbl2Criteria.add(Restrictions.eq("id", id));
Criteria tbl1Criteria = tbl2Criteria.createCriteria("tbl1Class");//assuming thats the name of the tbl1 instance in tbl2 class
tbl1Criteria.add(Restrictions.eq("someOtherThanKeyColumn", messageType));
tb12Criteria.add(Restrictions.or(categoryRestriction, strategyRestriction));
List<tbl2Class> result = tbl2Criteria.list();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM