I have the following entity relationships
Product
@Entity
@Table(name="PRODUCTS")
public class Product
@Id
@Column(name="product_id")
private long productId;
@ManyToOne
@JoinColumn(name="EMP_NUMBER")
private Employee employee3;
....
....
Employee
@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;
....
....
In DAOImpl class I have the following
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));
However when I execute, I am getting the following errors
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)
What I am trying to achieve is to generate the following SQL
SELECT p.product_id,
emp.emp_name
FROM products p
LEFT OUTER JOIN employees emp
ON (p.emp_number = emp.emp_number)
How can I do this and get rid of errors?
I have resolved the issue by adding the below mentioned line, might be useful to others if they encounter same problem what been mentioned in question.
((org.eclipse.persistence.jpa.JpaQuery)query).getDatabaseQuery().dontMaintainCache();
Another better option is provided here
Thanks
According to this , there are two ways to create a criteria query createQuery(class)
and createQuery()
without the parameter. The first method creates a CriteriaQuery cast the result to class
.
This means to resolve the problem, you can change CriteriaQuery<Product> cq = cb.createQuery(Product.class);
to this one CriteriaQuery<Product> cq = cb.createQuery();
.
And then use an Iterator
to retrieve the result.
Iterator products = query.getResultList().iterator();
In my case and according to this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=303205 The issue happens when you apply a multiselect and don't have a constructor with the selected parameters in the representation object.
For instance if you have
Select empNumber,employeeName from Employee
Then in your entity or representation object you need a constructor:
public Employee(int empNumber, StringemployeeName ){...}
Be careful since the order of the parameters matter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.