Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA criteria query load entire table
    text
    copied!<p>I feel like this is a silly question, but I can't find the answer. I have a class as follows:</p> <pre><code>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; } } </code></pre> <p>Now I want to use a criteria query to load the entire table (ie "select * from variables"). I'd like to use a criteria query more for code consistency than anything else. I get this exception though: </p> <pre><code>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 </code></pre> <p>The query I am using is:</p> <pre><code>public List&lt;Variable&gt; loadAllVariables() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery&lt;Variable&gt; query = builder.createQuery(Variable.class); return em.createQuery(query).getResultList(); } </code></pre> <p>I know that the exception means it wants this:</p> <pre><code>Root&lt;Variable&gt; variableRoot = query.from(Variable.class); </code></pre> <p>But without a Predicate I don't see how to get the Root object into the query?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload