Qua*_*ion 6 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();
Run Code Online (Sandbox Code Playgroud)
生成我需要的查询,然后是三个不需要的查询(strList中有多个值的附加查询).以下内容打印在日志中(我构建了第一个查询以将其分开.)第一个查询正是我想要的是接下来的三个查询,它们在生产中导致不需要的IO,我将其视为异常.请注意,如果in表达式不在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=?
Run Code Online (Sandbox Code Playgroud)
为什么额外三个查询?我该如何预防?我需要答案作为标准查询.
这是实体对象:
@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...
}
Run Code Online (Sandbox Code Playgroud)
下一个实体
@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...
}
Run Code Online (Sandbox Code Playgroud)
编辑 ~~~~~~~~~~~~~~~~~~~~~~~~解决方案~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~:
将第二行添加到上面的代码可以解决问题(谢谢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
Run Code Online (Sandbox Code Playgroud)
进行附加查询的原因是您正在使用字段: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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1069 次 |
| 最近记录: |