0

我正在尝试从“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
4

2 回答 2

0

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

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

于 2012-04-09T13:43:04.737 回答
0

尝试

SELECT * FROM Magazine x order by x.title asc, x.price desc

代替

SELECT x from Magazine x ...
于 2012-04-09T11:33:12.557 回答