Getting Column Names from a JPA Native Query

Getting column names from a JPA Native Query

After a long time without any answer, And based on my own further research, It seems that it can not be possible, Unfortunately.

Getting dynamically generated column names from result of Hibernate native query

Finally, found the solution, and I'm going to share with you(maybe helps someone). Here is what I did:

  • I added Spring JDBC Support to my application, then,
  • I used Spring SimpleJdbcTemplate Querying, there is a method: SimpleJdbcDaoSupport.getSimpleJdbcTemplate().queryForList(nativeQuery) which returns List<Map<String,Object>>

Spring - native queries mixed with hibernate when columns name are not the same as in the model

Note: you've duplicated the lastName field in your Entity, however I'll assume that's a typo because the compiler won't have allowed that in your real code.

All of the column & entity names match those in the actual table with the exception of lastName/last_name and id/user_id, so this becomes confusing when one looks at the @Query. The only place you're currently mixing names is the lastName/last_name element of your query, but I suspect it'll also complain when trying to map the result set to the Entity because of the id/user_id problem. (the answer there will be to be explicit about which columns you're retrieving and use a column alias to map user_id to id.)

Basically, the problem is in your question: you're trying to mix native & entity mapped names. You cannot - use one or the other.



Related Topics



Leave a reply



Submit