加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JpaRepository中的类型化方法未返回正确的类型

发布时间:2020-12-15 02:17:39 所属栏目:Java 来源:网络整理
导读:我在JpaRepository中有一个方法,它应该返回一个JPA实体列表: @Entitypublic class SomeEntity { // ...}@Repositorypublic interface SomeOtherEntityRepository extends JpaRepositorySomeOtherEntity,Long { @Query(value = "select some big long native
我在JpaRepository中有一个方法,它应该返回一个JPA实体列表:

@Entity
public class SomeEntity {
    // ...
}

@Repository
public interface SomeOtherEntityRepository extends JpaRepository<SomeOtherEntity,Long> {

    @Query(value = "select some big long native query",nativeQuery = true)
    public List<SomeEntity> getThings(String key);
}

当这个查询执行并且我尝试访问List中的条目时,我得到一个ClassCastException:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to my.package.SomeEntity
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at ...

当我调试时,这就是我所看到的:

enter image description here

这似乎打破了Java中的强类型变量.我不知道这是可能的.我可以更改我的代码以期望一个Object数组并将Object数组转换为实体,但这似乎是我不应该做的事情.

解决方法

JPA似乎不支持为@Query指定结果类.您可以使用@NamedNativeQuery或EntityManager.createNativeQuery来指定结果类.

见http://www.thoughts-on-java.org/jpa-native-queries/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读