I am building a system on top of an existing (old and messy) database. For this I'm building a JPA layer above the database, but I do not want to map my domain objects directly to the existing database tables.
I currently have a wrapper object that encapsulates 4 entities (data from 4 tables) in the database. Is there a way I can use this wrapper class as a parameter in a JPQL query?
It would be great if the wrapper class itself acted somewhat like a JPA entity, but without having the corresponding database table. Can this be achieved, and if so, how?
For example, how should I change Wrapper
public class Wrapper {
private FirstJpaEntity first;
private SecondJpaEntity second;
}
so that I can use something like
List<Wrapper> wrappers = ...;
TypedQuery<Wrapper> query = entityManager.createQuery(
"SELECT wrap" // This is probably where the problem lies: JPA needs a Type for `wrap`, but this type needs to be an @Entity which `wrap` is not
+ " WHERE wrap IN :wrappers"
+ " AND wrap.first.property = 1"
+ " AND wrap.second.property = 2"
, Wrapper.class);
query.setParameter("wrappers", wrappers);
I looked into making Wrapper an @Embeddable, hoping JPQL could figure out how to navigate through it, but alas @Embeddable objects cannot be queried directly.