0

假设这些是我的实体:

表1.java

@Entity
public class Table1 {

    // Many to one
    private Table2 table2;

    // Raw attributes
    // ...

}

表2.java

@Entity
public class Table2 {

    // Many to one
    private Table3 table3;

    // Raw attributes
    // ...

}

表3.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行。

4

1 回答 1

2

尝试这样的事情:

    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然后观看日志以查看休眠执行的实际选择。

于 2012-06-06T08:57:16.950 回答