Mysql 如何在Java中使用JPA检索表中的最后2条记录

Mysql 如何在Java中使用JPA检索表中的最后2条记录,mysql,eclipselink,Mysql,Eclipselink,我试图从“Execution”表中检索“Vulcano”表中最后5个插入的数据,我正在使用EclipseLink,Facade类,它现在调用这个查询 这里是Facade类am使用的一部分 public ArrayList<ExecutionPOJO> getLatestsVulcanoExecutions() { ArrayList<ExecutionPOJO> vulcanoExecutions = new ArrayList<ExecutionP

我试图从“Execution”表中检索“Vulcano”表中最后5个插入的数据,我正在使用EclipseLink,Facade类,它现在调用这个查询

这里是Facade类am使用的一部分

    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: 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 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
@实体
公共类Vulcano实现了可序列化{
...
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
伊德瓦卡诺私人酒店;
@OneToMany(targetEntity=Execution.class,mappedBy=“vulcano”,cascade=CascadeType.ALL)
私人清单处决;
公共列表getExecutions(){
返回执行;
}
公共执行(列表执行){
这个。执行=执行;
}
//其他的接球手和二传手
试试看

而不是

SELECT x from Magazine x ...

Limit不是JPQL的一部分,因此无法使用。相反,您可以在查询上调用setMaxResults来限制返回的结果


或者您可以使用本机SQL查询,其中Select*将使用EntityManager createNativeQuery api而不是createQuery来工作。

将无法使用star,因为它引发了一个错误;异常描述:语法错误解析查询[Select*from Execution s order by s.vulcano desc],第1行,第7列:意外标记[*]@user1321704:选择您想要阅读的列,如
select col1、col2、col3 from Magazine order by title asc、price desc
@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
SELECT * FROM Magazine x order by x.title asc, x.price desc
SELECT x from Magazine x ...