1

我有一个 JPA 实体,它在一个类MyEntity中包含一个复合主键。 我在方法中使用本机 sql 查询:@EmbeddableMyEntityPK
getThreeColumnsFromMyEntity()

 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".
我直接在数据库上测试了我的 sql 语句,它工作正常。
当我在 Eclipse 上运行它时,我得到一个 Oracle 错误:

ORA-00911 和“错误代码 911,查询:ResultSetMappingQuery [..]

我的猜测是映射有问题,但我不知道它是什么。

4

1 回答 1

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

于 2017-08-24T14:48:42.790 回答