Correct listing in ResultTransformer

I use a Hibernate result transformer to get a list of results from a database as follows:

Query query = em.createNativeQuery("SELECT 1 as myTrueBoolean, 2 as myInt") query.unwrap(org.hibernate.Query.class).setResultTransformer‌​(Transformers.aliasT‌​oBean(myDataClass)); query.fetchResultList() 

Than I could define a data class as follows:

 class MyDataClass { boolean myTrueBoolean; int myInt; } 

The problem is that the transformer will not display the data correctly, as it will assign BigInteger as boolean ( IllegalArgumentException occurred while calling setter for property [MyDataClass.myTrueBoolean (expected type = boolean)]; target = MyDataClass, property value = [0]] ), and the same with the appointment of BigInteger as int. This will not be a problem for shared Hibernate objects.

EDIT:

I am not looking for an explanation why this does not work. I am looking for a way to make it work :-) I need this for my native queries. Is there a way to implement my own transformer that would do this?

0
source share
1 answer

For your own SQL queries, you get what the database returns. This is a kind of point with your own SQL queries. For MySQL, it returns BigInteger , but for MsSQL it can return Integer .

Change your class to

 class MyDataClass { Number myTrueBoolean; Number myInt; } 

Another option is to use JPA queries. They will return value types from your objects.

0
source

Source: https://habr.com/ru/post/1268898/


All Articles