user1148956
user1148956

Reputation: 313

Is there a way with JPA CriteriaBuilder to fetch a non-entity object?

Say that I want to do a select from 2 databases. Database1 has fields A, B and C. Database2 has fields D, E and F. I have a POJO that consists of fields X, Y and Z. I want to write a CriteriaBuilder query to select all of the B, D and F fields in the result set and map them to the fields X, Y and Z in my POJO, returning a list of my POJO objects. Is this possible? I can do this with a native query, but I don't want to use native queries.

Upvotes: 0

Views: 1569

Answers (1)

Nayan Wadekar
Nayan Wadekar

Reputation: 11602

You can try using the below query to return a entity initialized by other entities :

SELECT NEW com.package.Entity3(e1.b, e2.d, e2.f) FROM Entity e1, Entity e2;

Then you have to provide a constructor with similar signature as in query, where you can set the field values as required.

public Entity3(int b, int d, int f){
   x = b;
   y = d;
   z = f;
}

Upvotes: 1

Related Questions