How to Return a List of Specific Type Instead of List<Object[]> in Hibernate

How to return a list of specific type instead of List<Object[]> in Hibernate?

There are different types of selects in JPA queries. You are currently using Array as a return type, what you need is Construct return type. Here is how to achieve this:

String queryStr =
"select NEW package.YourDefinedCustomClass(
a.field1, b.field2, c.field3, c.field4) from a left outer join b
on a.id=b.fk left outer join c on b.id=c.fk";

TypedQuery<YourDefinedCustomClass> query =
em.createQuery(queryStr, YourDefinedCustomClass.class);

List<YourDefinedCustomClass> results = query.getResultList();

Basically there are two things:

  1. Custom class must be your results return type
  2. Custom class must have a constructor which takes result values you define in query string.

Read more on selects in JPA2 queries.

What is the proper way to cast Hibernate Query.list() to List<Type>?

Short answer @SuppressWarnings is the right way to go.

Long answer, Hibernate returns a raw List from the Query.list method, see here. This is not a bug with Hibernate or something the can be solved, the type returned by the query is not known at compile time.

Therefore when you write

final List<MyObject> list = query.list();

You are doing an unsafe cast from List to List<MyObject> - this cannot be avoided.

There is no way you can safely carry out the cast as the List could contain anything.

The only way to make the error go away is the even more ugly

final List<MyObject> list = new LinkedList<>();
for(final Object o : query.list()) {
list.add((MyObject)o);
}

Hibernate native query return List Object List

Yes, hibernate will return the Object arrays (Object[]) for this case - return multiple columns. but you still can using the "Entity queries" to return a entity objects rather then the "raw" value.

Hibernate: how to return map<String, List<Object>>

You can retrieve all the products as a List<Product> in one query using

TypedQuery<Product> query = em.createQuery("select p from Product p", Product.class);
List<Product> allProducts = query.getResultList();

and then organize them:

Map<String, List<Product>> productsByArticle = allProducts.stream()
.collect(Collectors.groupingBy(Product::getArticle));

That should be reasonably efficient as it only involves a single query, and assuming you need all the data anyway, you need to get it somehow. The "grouping by" operation is more or less linear in the number of products (I think), and likely the time taken is negligible compared to executing the query.

(Obviously, you can do this in a single shot, though it's doing exactly the same thing):

TypedQuery<Product> query = em.createQuery("select p from Product p", Product.class);
Map<String, List<Product>> productsByArticle = query.getResultList().stream()
.collect(Collectors.groupingBy(Product::getArticle));

Cast Hibernate result to a list of objects

You need to specify the class of entity the result should be converted to using addEntity(), because you are executing SQL query that doesn't know anything about entities:

List<Associate> associate = (List<Associate>) session.createSQLQuery(
"SELECT * FROM associates WHERE fk_id = :id AND fk_associate_id = (SELECT id FROM users WHERE fk_user_type = 2)")
.addEntity(Associate.class)
.setParameter("id", id).list();

See also:

  • 18.1.2. Entity queries

EntityManager.createNativeQuery returning list of objects instead of list of BigDecimal when using Pagination

After a lot of trails with different versions of different spring libraries, I was finally able to figure out the issue. In one of my attempts, the issue seems to have disappeared, as soon as I updated the spring-data-commons library from v2.1.5.RELEASE to v2.1.6.RELEASE. I looked up the changelog of this release, and this bug, which is related to this bug in spring-data-commons, is the root cause of this issue. I was able to fix the issue after upgrading the spring-data-commons library.



Related Topics



Leave a reply



Submit