我觉得这是一个愚蠢的问题,但我找不到答案。我有一堂课如下:
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;
}
}
现在我想使用条件查询来加载整个表(即“从变量中选择 *”)。我想更多地使用标准查询来实现代码一致性,而不是其他任何东西。我得到了这个例外:
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
我正在使用的查询是:
public List<Variable> loadAllVariables()
{
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Variable> query = builder.createQuery(Variable.class);
return em.createQuery(query).getResultList();
}
我知道异常意味着它想要这个:
Root<Variable> variableRoot = query.from(Variable.class);
但是没有谓词,我看不到如何将 Root 对象放入查询中?