JPA2条件在select语句中查询FK中的in-expression,其中n是#values

时间:2011-01-25 23:57:05

标签: hibernate jpa jpa-2.0

我对使用针对DB2和MySQL的hibernate也有同样的问题。

这是一个测试:

        EntityManager em = emf.createEntityManager();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Customers> query = cb.createQuery(Customers.class);
        Root<Customers> root = query.from(Customers.class);

        ArrayList<String> strList = new ArrayList<String>();
        strList.add("ADMIN");
        strList.add("SYSADMIN");
        strList.add("SALES");

        ArrayList<Predicate> predicateList = new ArrayList<Predicate>();

        Path<Groups> groups = root.get(Customers_.groups);
        Path<String> groupName = groups.get(Groups_.name);
        In<String> in = cb.in(groupName);
        for (String s : strList) { //has a value
            in = in.value(s);
        }
        predicateList.add(in);
        Predicate[] predicates = new Predicate[predicateList.size()];

        query.where(predicateList.toArray(predicates));
        TypedQuery<Customers> typedQuery = em.createQuery(query);
        this.outList = typedQuery.getResultList();

生成我需要的查询,然后是三个不需要的查询(strList中有多个值的附加查询)。以下内容打印在日志中(我构建了第一个查询以将其分开。)第一个查询正是我想要的是接下来的三个查询,它们在生产中导致不需要的IO,我将其视为异常。请注意,如果 in expression 不在FK上,则不会发生此问题。

INFO: Hibernate: 
select 
  customers0_.id as id0_, customers0_.fname as fname0_, customers0_.groups as groups0_, customers0_.lname as lname0_ 
from 
  test.customers customers0_ 
where 
  customers0_.groups in (? , ? , ?)

INFO: Hibernate: select groups0_.name as name1_0_ from test.groups groups0_ where groups0_.name=?
INFO: Hibernate: select groups0_.name as name1_0_ from test.groups groups0_ where groups0_.name=?
INFO: Hibernate: select groups0_.name as name1_0_ from test.groups groups0_ where groups0_.name=?

为什么额外的三个查询?我该如何预防?我需要答案作为标准查询。


以下是实体对象:

@Entity
@Table(name = "customers", catalog = "test", schema = "")
public class Customers implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Column(name = "fname", length = 45)
    private String fname;
    @Column(name = "lname", length = 45)
    private String lname;
    @JoinColumn(name = "groups", referencedColumnName = "name")
    @ManyToOne
    private Groups groups;

...getters and setters... 
}

下一个实体

@Entity
@Table(name = "groups", catalog = "test", schema = "")
public class Groups implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "name", nullable = false, length = 45, unique = true)
    private String name;
    @OneToMany(mappedBy = "groups")
    private Collection<Customers> customersCollection;

...getters and setters... 
}

编辑 ~~~~~~~~~~~~~~~~~~~~~~~~~解决方案~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~:

在上面的代码中添加第二行可以解决问题(谢谢Clement):

Root<Customers> root = query.from(Customers.class);
root.fetch(Customers_.groups, JoinType.LEFT); //that's it that's all now it will not create the extra queries

1 个答案:

答案 0 :(得分:2)

附加查询的原因是因为您使用字段private Groups groups;进行从客户到组的关联。由于hibernate无法拦截直接字段访问,因此在获取Customer时必须获取Groups对象。它以N + 1选择方式对标准查询执行此操作(而不是,例如,计算出自动连接或子选择)。

要解决这个问题,你可以告诉hibernate获取关联:

root.fetch(Customers_.groups, JoinType.LEFT); // LEFT join since your schema could have a customer with a null group.