Java – the typed method in the jparepository does not return the correct type

I have a method in the jparepository that should return a list of JPA entities:

@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);
}

When the query executes and I try to access the entries in the list, I get a ClassCastException:

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

When I debug, this is what I see:

This seems to break the strongly typed variables in Java I didn't know it was possible I can change my code to expect an object array and convert the object array to an entity, but this seems to be something I shouldn't do

Solution

JPA does not seem to support specifying a result class for @ query You can use @ namednatequery or entitymanager Createnativequery to specify the result class

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>