JPA criteria criteria query loads the entire table

I feel this is a stupid question, but I cannot find the answer. I have a class as follows:

import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import javax.persistence.Table; @Entity @Table(name="DEMO_VARIABLES") public class Variable implements Serializable { private static final long serialVersionUID = -1734898766626582592L; @Id @SequenceGenerator(name="VARIABLE_ID_GENERATOR", sequenceName="DEMO_VARIABLE_ID_SEQ", allocationSize=1) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="VARIABLE_ID_GENERATOR") @Column(name="VARIABLE_ID", unique=true, nullable=false, precision=22) private long variableId; @Column(name="VARIABLE_NAME", nullable=false, length=50) private String variableName; @Column(name="VARIABLE_VALUE", nullable=false, length=500) private String variableValue; public Variable() { } public long getVariableId() { return variableId; } public void setVariableId(long variableId) { this.variableId = variableId; } public String getVariableName() { return variableName; } public void setVariableName(String variableName) { this.variableName = variableName; } public String getVariableValue() { return variableValue; } public void setVariableValue(String variableValue) { this.variableValue = variableValue; } } 

Now I want to use the criteria query to load the whole table (ie "select * from variables"). I would like to use the criteria query more for code consistency than anything else. I get this exception:

 java.lang.IllegalStateException: No criteria query roots were specified at org.hibernate.ejb.criteria.CriteriaQueryImpl.validate(CriteriaQueryImpl.java:303) at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:145) at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:437 

I am using the following query:

 public List<Variable> loadAllVariables() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Variable> query = builder.createQuery(Variable.class); return em.createQuery(query).getResultList(); } 

I know that an exception means that he wants this:

 Root<Variable> variableRoot = query.from(Variable.class); 

But without the predicate, I don’t see how to get the Root object in the request?

+4
source share
2 answers

I am not sure if I understood you correctly, but if the goal is to select a selection list of all Variable objects, then follow as follows:

 public List<Variable> loadAllVariables() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Variable> query = builder.createQuery(Variable.class); Root<Variable> variableRoot = query.from(Variable.class); query.select(variableRoot); return em.createQuery(query).getResultList(); } 

The difference is that choice is used. All implementations implicitly use the last call from instead of select. In the JPA 2.0 specification, it is said as follows:

Portable applications should use the select or multiselect method to specify a query select list. Applications that do not use one of these methods will not be portable.

+16
source

Working with a query and its root is very simple:
First enter the root as you described:

 Root<Variable> variableRoot = query.from(Variable.class); 

Subsequently, you must indicate the query what it should do with the root: select in this case.

 query.select(variableRoot); 

After that, you can further modify the query using the query.where(...) function, etc. When you are done, you are ready to launch it with

 return em.createQuery(query).getResultList(); 

More info in Oracle tutorial

+4
source

All Articles