Search code examples
javajpaeclipselinkjpa-2.0criteria-api

JPA left outer join using CriteriaBuilder results in error: Partial object queries are not allowed to maintain the cache or be edited


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?


Solution

  • 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