我正在尝试从'Execution'表中检索'Vulcano'表中的最后5个插入数据,我正在使用EclipseLink,Facade类,此时调用此查询;
这是Facade类使用的一部分;
public ArrayList<ExecutionPOJO> getLatestsVulcanoExecutions() {
ArrayList<ExecutionPOJO> vulcanoExecutions = new ArrayList<ExecutionPOJO>();
//SELECT x FROM Magazine x order by x.title asc, x.price desc
**List<Execution> excutionVulcanoList = em.createQuery("select s from Execution s order by s.vulcano_idVulcanoID desc limit 2 ").getResultList();**
如果我使用上述查询,我会收到此错误:
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select s from Execution s order by s.vulcano desc limit 2], line 1, column 50: syntax error at [limit].
Internal Exception: MismatchedTokenException(80!=-1)
但如果删除限制标记,我会收到此错误;
Caused by: Exception [EclipseLink-8021] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [select s from Execution s order by s.vulcano desc], line 1, column 36: invalid ORDER BY item [s.vulcano] of type [uk.ac.aston.tomtom.entity.Vulcano], expected expression of an orderable type.
@Entity
public class Execution implements Serializable {
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idExecution;
@ManyToOne(targetEntity = Vulcano.class)
private Vulcano vulcano;
*//Few others @ManyToOne
//also @oneToMany*
public Vulcano getVulcano() {
return vulcano;
}
public void setVulcano(Vulcano vulcano) {
this.vulcano = vulcano;
}
//other getter and setter
}
这是另一个实体;
@Entity
public class Vulcano implements Serializable {
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idVulcano;
@OneToMany(targetEntity=Execution.class, mappedBy="vulcano", cascade=CascadeType.ALL)
private List<Execution> executions;
public List<Execution> getExecutions() {
return executions;
}
public void setExecutions(List<Execution> executions) {
this.executions = executions;
}
//other getters and setters
答案 0 :(得分:0)
尝试
SELECT * FROM Magazine x order by x.title asc, x.price desc
而不是
SELECT x from Magazine x ...
答案 1 :(得分:0)
限制不是JPQL的一部分,因此无法使用。相反,您可以在查询上调用setMaxResults来限制返回的结果。
或者您可以使用本机SQL查询,其中Select *将使用EntityManager createNativeQuery api而不是createQuery。