Stefan
Stefan

Reputation: 1511

JPA: Map a group of table columns extracted by native query to entity

I know that it is possible using @SqlResultSetMapping but I want to select not whole entity from the database but some fields and then map i to my entity using one of the constructor which accept that fields. Is that possible to map result with @EntityResult for only a few @FieldResult? I was trying to do that and all the time I get error which said that there is not specify mapping for some fields which exist in that entity.

Upvotes: 1

Views: 537

Answers (2)

Matteo Baldi
Matteo Baldi

Reputation: 5828

Well, if you are using JPA 1.0 your only option (not considering the manual mapping, of course), is to use @SqlResultSetMapping and map the whole table columns. With JPA 2.1 you can add a javax.persistence.ConstructorResult (see docs here) to map only the needed columns.

Upvotes: 1

Nishant
Nishant

Reputation: 157

The disadvantage of @SqlResultSetMapping is that you have to select all the columns.

The alternate way of doing this manually iterate over the DB result and populate your objects.

Upvotes: 1

Related Questions