2017-08-24 82 views
1

我有一個JPA實體MyEntity其中包括一個@EmbeddableMyEntityPK的複合主鍵。
我使用的是原生SQL查詢的方法getThreeColumnsFromMyEntity()JPA原生sql查詢映射錯誤?

public List<MyEntity> getThreeColumnsFromMyEntity() { 

    List<MyEntity> results = em.createNativeQuery("select pid,name,dateofbirth from (select pid,name, dateofbirth,max(dateofbirth) " 
      + "over(partition by pid) latest_dateofbirth from my_entity_table) where" 
      + " dateofbirth = latest_dateofbirth;","myEntityMapping").getResultList(); 

    return results; 

@SqlResultSetMapping

@SqlResultSetMapping(
    name = "myEntityMapping", 
    entities = { 
     @EntityResult(
       entityClass = MyEntityPK.class, 
       fields = { 
        @FieldResult(name = "PID", column = "pid"), 
        @FieldResult(name = "NAME", column = "name")}), 
     @EntityResult(
       entityClass = MyEntity.class, 
       fields = { 
        @FieldResult(name = "dateofbirth", column = "dateofbirth")})}) 

我JPA列被命名爲:@Column(name="DATEOFBIRTH")"PID""NAME"
我直接在db上測試了我的sql語句,它工作正常。
當我在Eclipse中運行它,我得到一個Oracle錯誤:

ORA-00911 and "Error code 911 , Query: ResultSetMappingQuery [..]

我的猜測是有一些錯誤的映射,但我找不到它是什麼。

+1

經常檢查什麼在這種情況下,Oracle錯誤信息是'ORA-00911:無效字符' 這給你一個啓動你的調查的提示 –

回答

1

我假設,因爲你缺少子查詢的別名,你得到這個錯誤,所以不是你可以試試這個:

select 
    pid, 
    name, 
    dateofbirth 
from 
    (
     select 
     pid, 
     name, 
     dateofbirth, 
     max(dateofbirth) over(partition by pid) AS latest_dateofbirth 
     from 
     my_entity_table 
    ) second_result 
--  ^--------------- use an aliase name to the subquery 
where 
    second_result.dateofbirth = latest_dateofbirth 
-- ^----use the aliase name to reference to any of its fields, in your case 'dateofbirth' 

看看有關錯誤的意思是這裏ORA-00911: invalid character tips