Reputation: 375
If I have a complex where clause which varies only slightly between many queries, in SQL I would create a view based on most of the where clause, then query the view multiple times. Both for performance and maintainability.
I don't see an equivalent of that in jpql.
Am I or the jpql spec. missing something?
Cheers, Phil
Upvotes: 1
Views: 2475
Reputation: 570515
Not a direct answer but why don't you map an Entity on an SQL view? In some cases, SQL views are the easiest and most effective solution, especially when the data are read-only. Just don't abuse them.
Upvotes: 1
Reputation: 299048
In JPA2, the combination of EntityManager and CriteriaQuery is very powerful:
http://java.sun.com/javaee/6/docs/api/javax/persistence/criteria/CriteriaQuery.html
You define a basic CriteriaQuery (somewhat like a view) using the CriteriaBuilder and then use the entitymanager to create a query based on the criteriaquery. Since criteriaquery are objects, they are reuseable on the java side.
Possibly providers also implement caching against criteriaqueries, but I don't know about that.
Upvotes: 0