I have created a native custom query method:
x
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?
Advertisement
Answer
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();