繁体   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