2012-04-09 6 views
0

'Execution'テーブルから 'Vulcano'テーブルの最後の5個のデータを取得しようとしています。瞬間;JavaでJPAを使用してテーブル内の最後の2レコードを取り出す方法

ここでは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

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

代わりに

SELECT x from Magazine x ... 
+0

エラーを投げたので、星を使用することはできません。 例外の説明:クエリを解析する構文エラー[select * from Execution s order by s.vulcano desc]、行1、列7:予期しないトークン[*]。 – user1321704

+0

@ user1321704: 'asc、price descで雑誌の注文からcol1、col2、col3を選択してください –

0

リミットJPQLの一部ではないので、使用できませんしてみてください。代わりに、クエリのsetMaxResultsを呼び出して返される結果を制限します。

またはSelect *がcreateQueryではなくEntityManagerのcreateNativeQuery APIを使用して動作するネイティブSQLクエリを使用できます。

関連する問題