Tuesday 26 November 2013

Native Query Mapping to Transient Fields of Entity class

Native queries are often a problem when comes to mapping with customized Entity classes. Especially classes with transient fields. To overcome this problem we have a special annotation available in JPA since version 1.0.
                                      @SqlResultSetMapping
That will do the trick for you!
This type of mapping is called scalar mapping. The only problem in choosing JPA is, the community is very limited and very limited resources are available internet. For complex JPA queries, most struggle to find a solution.
So I am going to show an example of mixing entity and scalar mapping.

@ColumnResult(name = "progress") does the scalar mapping. Remember that when you write your native query, It must have a column with alias progress.

For example: select p.*, count(*) as progress from project p
We can also use @SqlResultSetMapping to map different Entities from one native query. I will show this example in my next post.