question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createNativeQuery(queryString,Tuple.class).getResultList() method is not returning list of Tuple data

See original GitHub issue

I am using reactive hibernate (1.0.0.CR7) for my spring webflux project. I am getting the object array data while executing native queries and it is working fine. But I need it as a tuple object. So I passed Tuple.class as a second argument to createNativeQuery method. But it will throw an exception as it cannot cast an object to tuple. I just need the data with column alias name. So I choose tuple instead of object array. Kindly help.

Sample code :

String queryString= "select c.country_name as country_name, c.phone_code as phone_code
from erp_country c"; // MySql query
Mutiny.Query<Tuple> query = session.createNativeQuery(queryString, Tuple.class);
List<Tuple> list = query.getResultList().await().indefinitely();
for (Tuple tuple : list) { // here it will throw an exception as object cannot be cast to class javax.persistence.Tuple
     System.out.println(tuple.get("country_name") + "-" + tuple.get("phone_code"));
}

Exception Message:

Ljava.lang.Object; cannot be cast to class javax.persistence.Tuple ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; javax.persistence.Tuple is in unnamed module of loader 'app')

I have another project using JPA- hibernate 5.5. There I can pass the second parameter of createNativeQuery as tuple class instead of giving an entity class. So I thought may be there is a chance to use Tuple in reactive hibernate also. If we use tuple class then we can retrieve the data by using alias name in the query string.

Few Reference :

https://stackoverflow.com/questions/16296853/jpa-result-list-cast-object-to-tuple https://stackoverflow.com/questions/44626609/getting-column-names-from-a-jpa-native-query

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DavideDcommented, Aug 2, 2021

Hah. I did not know (or once knew and forgot) that this world in plain Hibernate.

I had the same exact reaction 😃

Thanks @nivinps

1reaction
gavinkingcommented, Aug 2, 2021

Hah. I did not know (or once knew and forgot) that this worked in plain Hibernate.

But if it does, then it should be pretty easy to make it work in HR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot use Tuple class with native queries - Stack Overflow
Query q = em.createNativeQuery(QUERY_STRING, Tuple.class); q.setParameter(1, param1); q.getResultList();. But I receive the following error ...
Read more >
getResultList() not returning Tuple · Issue #778 - GitHub
Javadoc of EntityManager.createQuery explicitly says: The select list of the query must contain only a single item, which must be assignable to ...
Read more >
Native SQL Queries
These will return a List of Object arrays ( Object[] ) with scalar values for each column in the PERSON table. Hibernate will...
Read more >
JPA + Hibernate - Tuple example in JPQL - LogicBig
We can use following method of EntityManager to create Tuple query: public <T> TypedQuery<T> createQuery(String query, Class<T> resultClass);.
Read more >
How to call native SQL queries with JPA & Hibernate
It returns an implementation of the Query interface, which is the same that you get when you call the createQuery method to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found