【问题标题】:JPA native sql query mapping error?JPA本机sql查询映射错误?
【发布时间】:2017-08-24 14:30:14
【问题描述】:

我有一个 JPA 实体 MyEntity,它在 @EmbeddableMyEntityPK 中包含一个复合主键。
我在方法 getThreeColumnsFromMyEntity() 中使用原生 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"
我直接在数据库上测试了我的 sql 语句,它工作正常。
当我在 Eclipse 上运行它时,我得到一个 Oracle 错误:

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

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

【问题讨论】:

  • 始终检查 Oracle 错误消息是什么,在本例中为 ORA-00911: invalid character 这为您提供了开始调查的提示

标签: java oracle jpa native-sql


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 2013-01-22
    • 2014-06-25
    • 2019-04-05
    • 2014-01-02
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多