gpt4 book ai didi

java - JPA native sql查询映射错误?

转载 作者:行者123 更新时间:2023-11-30 06:32:15 29 4
gpt4 key购买 nike

我有一个 JPA 实体 MyEntity,其中包含 @EmbeddableMyEntityPK 中的复合主键。
我在方法 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com