简体   繁体   中英

Spring Data JPA – Custom Native Query-Methods list prints java.lang.Object

I have created a native custom query method:

 public List<Product> test() {
       return (em.createNativeQuery("SELECT * from products")).getResultList();
 }

And calling EntityManager with:

@PersistenceContext
private EntityManager em;

But all I get is:

[[Ljava.lang.Object;@3e9645ea, [Ljava.lang.Object;@a4d8d28, [Ljava.lang.Object;@402a1b8d, [Ljava.lang.Object;@3e654fce, [Ljava.lang.Object;@3250e4fd, [Ljava.lang.Object;@54921b52]

when I print out the list. Number of objects is correct so I assume something with casting is not correct. I do get a warning in my IDE:

Unchecked assignment: 'java.util.List' to 'java.util.List<packageName.Product>' 

Why is this happening and how do I get the correct list?

You can use the expected type in the createNativeQuery method like below,

query = entityManager.createNativeQuery("SELECT * from products", Product.class)
List<Product> results = query.getResultList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM