I am trying to make a native named query.I saw the link
result-set-mapping-complex-mappings
<sql-result-set-mapping name="BookAuthorMappingXml">
<entity-result entity-class="org.thoughts.on.java.jpa.model.Author">
<field-result name="id" column="authorId"/>
<field-result name="firstName" column="firstName"/>
<field-result name="lastName" column="lastName"/>
<field-result name="version" column="authorVersion"/>
</entity-result>
<entity-result entity-class="org.thoughts.on.java.jpa.model.Book">
<field-result name="id" column="id"/>
<field-result name="title" column="title"/>
<field-result name="author" column="author_id"/>
<field-result name="version" column="version"/>
</entity-result>
</sql-result-set-mapping>
the number of columns i have is more than 20.Is there is way to map all columns in one go
I am using hibernate 4.2
Hibernate provide option to execute native SQL queries through the use of SQLQuery object. Hibernate SQL Query is very handy when we have to execute database vendor specific queries that are not supported by Hibernate API.
The easiest way to map a query result to an entity is to provide the entity class as a parameter to the createNativeQuery(String sqlString, Class resultClass) method of the EntityManager and use the default mapping.
JPQL is the most commonly used query language with JPA and Hibernate. It provides an easy way to query data from the database. But it supports only a small subset of the SQL standard, and it also does not support database-specific features. If you want to use any of these features, you need to use a native SQL query.
Annotation Type SqlResultSetMappingSpecifies the mapping of the result of a native SQL query or stored procedure.
What you need is an hibernate mapping auto generation. You can follow the blog from mkyong
https://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With