簡體   English   中英

Hibernate:通過示例查詢和多對一關系

[英]Hibernate: queries by example and many-to-one relationships

讓我們說這些是我的實體:

Table1.java

@Entity
public class Table1 {

    // Many to one
    private Table2 table2;

    // Raw attributes
    // ...

}

Table2.java

@Entity
public class Table2 {

    // Many to one
    private Table3 table3;

    // Raw attributes
    // ...

}

Table3.java

@Entity
public class Table3 {

    // Raw attributes
    private String lang; // "fr", "en", "de"...

}

我想列出所有Table1行,其中table2.table3.lang等於en 我嘗試通過示例使用查詢:

Table3 table3Example = new Table3();
table3Example.setLang("en");

Table2 table2Example = new Table2();
table2Example.setTable3(table3Example);

Table1 table1Example = new Table1();
table1Example.setTable2(table2Example);

table1Repository.findByExample(table1Example);

問題是.findByExample(table1Example)返回數據庫的所有行,無論是lang ,這意味着根本不考慮過濾器:(

任何幫助將不勝感激:)

PS:沒有拋出異常, .findByExample(table1Example)只返回所有Table1行。

嘗試這樣的事情:

    Query q = entityManager.createQuery("Select o from Table1 o where o.table2.table3.lang = :lang");
    q.setParameter("lang", "en");
    List<Table1> r = (List<Table1>)q.getResultList();

要了解為什么要獲取Table1中的所有行,請確保擁有

<property name="hibernate.show_sql" value="true"/>

在你的persistence.xml ,然后觀察日志以查看hibernate執行的實際選擇。

暫無
暫無

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

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