我有一个 JPA 实体 MyEntity
,其中包含 @Embeddable
类 MyEntityPK
中的复合主键。
我在方法 getThreeColumnsFromMyEntity()
中使用 native 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 and "Error code 911 , Query: ResultSetMappingQuery [..]
我猜测映射有问题,但我无法找出问题所在。
最佳答案
我假设您收到此错误是因为您缺少子查询的别名,因此您可以尝试以下操作:
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
关于java - JPA native sql查询映射错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45864294/