6

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

coder25
  • 2,363
  • 12
  • 57
  • 104
  • Are you looking for something like http://stackoverflow.com/a/17210746/2646526? – heenenee Apr 02 '17 at 21:01
  • yes but using named queries and method like defined in question – coder25 Apr 03 '17 at 04:13
  • something like this? https://github.com/kiegroup/jbpm/blob/master/jbpm-persistence/jbpm-persistence-jpa/src/main/resources/META-INF/JBPMorm.xml – Alex Chernyshev Apr 06 '17 at 11:13
  • 1
    You could create a new Entity class that has all the fields you want and not bother describing each fieldresult. That way instead of an `Object[]` each row would give you a well-described class you won't even have to typecast. – coladict Apr 07 '17 at 10:23
  • I dont want to create a new entity class as the number of fields are too many and this will make me change the entire references – coder25 Apr 07 '17 at 10:26
  • Well, then the answer to your question is "no". There is no short way to do it. You'll have to describe it in the long and harder to maintain way. – coladict Apr 07 '17 at 10:31

1 Answers1

2

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/

Abdul Rahman
  • 446
  • 2
  • 8