gpt4 book ai didi

java - 使用 CriteriaBuilder 的 JPA 左外部联接导致错误 : Partial object queries are not allowed to maintain the cache or be edited

转载 作者:行者123 更新时间:2023-11-30 08:56:52 25 4
gpt4 key购买 nike

我有以下实体关系

产品

@Entity
@Table(name="PRODUCTS")
public class Product

@Id
@Column(name="product_id")
private long productId;

@ManyToOne
@JoinColumn(name="EMP_NUMBER")
private Employee employee3;
....
....

员工

@Entity
@Table(name="EMPLOYEES")
public class Employee

@Id
@Column(name="EMP_NUMBER")
private String empNumber;

@Column(name="EMP_NAME")
private String employeeName;

@OneToMany(mappedBy="employee3")
private List<Product> Product3;

....
....

在 DAOImpl 类中我有以下内容

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Product> cq =
cb.createQuery(Product.class);
Root<Product> c = cq.from(Product.class);
Join<Product, Employee> join =
c.join(Product_.employee3, JoinType.LEFT);
cq.multiselect(c.get(Product_.productId),
c.get(Product_.employee3));

但是当我执行时,出现以下错误

org.eclipse.persistence.exceptions.QueryException Exception Description: Partial object queries are not allowed to maintain the cache or be edited.
You must use dontMaintainCache(). Query: ReadAllQuery(referenceClass=Product ) at org.eclipse.persistence.exceptions.QueryException.cannotCachePartialObjects (QueryException.java:388) at org.eclipse.persistence.queries.ObjectLevelReadQuery.prepareQuery (ObjectLevelReadQuery.java:2160)

我想要实现的是生成以下 SQL

SELECT p.product_id,
emp.emp_name
FROM products p
LEFT OUTER JOIN employees emp
ON (p.emp_number = emp.emp_number)

我该如何做到这一点并消除错误?

最佳答案

我已经通过添加下面提到的行解决了这个问题,如果其他人遇到与上述问题相同的问题,可能对他们有用。

((org.eclipse.persistence.jpa.JpaQuery)query).getDatabaseQuery().dontMaintainCache();

提供了另一个更好的选择here

谢谢

关于java - 使用 CriteriaBuilder 的 JPA 左外部联接导致错误 : Partial object queries are not allowed to maintain the cache or be edited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28404924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com