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
Query query = em.createNativeQuery(sql.toString(), "SomeEntityQuery");
then we created an entity called SomeEntity which has a code like the one below
@SqlResultSetMapping(
name="SomeEntityQuery",
entities={
@EntityResult(
entityClass=SomeEntity.class,
fields={
@FieldResult(name="fieldOne", column="FieldOne"),
@FieldResult(name="fieldTwo", column="FieldTwo"),
@FieldResult(name="fieldThree", column="FieldThree")
}
)
}
)
@Entity
public class SomeEntity{
//fields listed below
My question is, what if we have a dao method that has the following methods that we translated to native queries
methodA()
which returns SomeEntityObject but only fills up fieldOne
, fieldTwo
, fieldThree
methodB()
which returns SomeEntityObject
but only fills up fieldFour
, fieldFive
, fieldSix
we noticed that when creating @SqlResultSetMapping
, 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?
The sql for that is...
SELECT a.someColumn as FieldOne , b.someColumn as FieldTwo , c.someColumn as FieldThree...etc..etc..