Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate native query mapping

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

like image 232
coder25 Avatar asked Mar 30 '17 11:03

coder25


People also ask

Does hibernate support native sql queries?

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.

How do you map native query results to entities?

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.

What is the difference between JPQL and native query?

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.

What is the use SqlResultSetMapping?

Annotation Type SqlResultSetMappingSpecifies the mapping of the result of a native SQL query or stored procedure.


1 Answers

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/

like image 88
Abdul Rahman Avatar answered Oct 02 '22 04:10

Abdul Rahman