JPA原生sql查询映射错误?
问题描述:
我有一个JPA实体MyEntity
其中包括一个@Embeddable
类MyEntityPK
的复合主键。
我使用的是原生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 [..]
我的猜测是有一些错误的映射,但我找不到它是什么。
答
我假设,因为你缺少子查询的别名,你得到这个错误,所以不是你可以试试这个:
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
经常检查什么在这种情况下,Oracle错误信息是'ORA-00911:无效字符' 这给你一个启动你的调查的提示 –