Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA criteria query load entire table

I feel like this is a silly question, but I can't 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 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:

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

The query I am using is:

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

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

I know that the exception means it wants this:

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

But without a Predicate I don't see how to get the Root object into the query?

like image 368
Greg Avatar asked Jul 26 '12 13:07

Greg


2 Answers

I am not sure, did I understood you right, but if goal is to choose fetch list of all Variable entities, then following is way to go:

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();
} 

Difference is that select is used. All implementations do not implicitly use last call of from in place of select. In JPA 2.0 specification this is told as follows:

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

like image 62
Mikko Maunu Avatar answered Nov 02 '22 07:11

Mikko Maunu


Working with the query and its root is really simple:
First get the root the way you described:

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

Afterwards, you have to tell the query what it should do with the root: select, in this case.

query.select(variableRoot);

After that, you can modify the query further with the query.where(...) function and such. When you're finished, you are ready to run it with your

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

More info in the Oracle tutorial

like image 32
Humungus Avatar answered Nov 02 '22 05:11

Humungus