FoolischEx
FoolischEx

Reputation: 79

Convert Objects from Query to other Class in Hibernate?

I have following problem: I am using Spring + Hibernate and am currently trying to select an id + amount of its id and return a list with each id and its corresponding amount. Now the problem is, that apperently the converter has a problem as I get following exception :

ConverterNotFoundException: No converter found capable of converting from type [java.lang.Long] to type [...entity.E_Amount]] with root cause

E_Amount creates Obejects with an id and an amount. And My Query:

 @Query("select t.e.id, count(t.id) from...")

returns a perfectly fine result of a long and an integer e.g. {9,101}

I am simply not completely sure how to start tackling this converting problem. Any ideas would be greatly appreciated!

Upvotes: 0

Views: 417

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

Try the result class strategy:

1) Create a new class ResultClass with constructor accepting your query results:

public class ResultClass{

    public ResultClass(Integer id, Long count){..}
}

2) Amend the query a bit:

@Query("select new com.mypackage.ResultClass(t.e.id, count(t.id)) from...")
public List<ResultClass> query(..);

Just make sure that constructor arguments are in the same order as column in the select statement.

Upvotes: 1

Related Questions