• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Reusing fields for JPA entities in native query

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We use JPA in our project but there are certain complex queries that need to be done straightforward in native query. We make use JPA native queries for this, like the one below



then we created an entity called SomeEntity which has a code like the one below







My question is, what if we have a dao method that has the following methods that we translated to native queries

which returns but only fills up fieldOne, fieldTwo, fieldThree

which returns but only fills up fieldFour, fieldFive, fieldSix

we noticed that when creating , all fields need to be defined or else an exception would be thrown if not.

What if the dao methods (which has native queries) stretches up to methodE() and we don't want to have a different Entity return for every dao method? we would want to use the same SomeEntity object for all.

Is there a workaround for my dilemma?

By the way, the sql statement for the above example is:

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

alrem mashayekhi wrote:What if the dao methods (which has native queries) stretches up to methodE() and we don't want to have a different Entity return for every dao method? we would want to use the same SomeEntity object for all.

Is there a workaround for my dilemma?


When I have some doubts/questions about JPA, I always have a look at the (excellent) Java Persistence WikiBook. I think the Native SQL Queries section might be very useful for you.

It seems you could try using the @SqlResultSetMapping annotation together with the @ConstructorResult annotation as explained in this article. I have never tried (used) it myself, so I have no idea if it will solve your problem. As a last resort you could always create a few helper methods to do a manual mapping of a list of Object[] elements to a list of SomeEntity objects with the appropriate fields being set.

Hope it helps!
Kind regards,
Roel
 
Saloon Keeper
Posts: 22246
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an app that does stuff like that (actually, there's very little that JPA can do that it hasn't ended up doing sooner or later).

One solution is not to assign an Entity class to your SqlResultSetMapping, let it return a list of lists of Objects (one per column), instantiate your own mini-entity instances transferring values from the list.

I would be cautious using a one-size-fits-all return entity, though, since parts of it would only be valid in certain contexts and you'd have to keep that in mind. Plus, dead fields take up memory space when returning a result list.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic