Is there an equivalent of views in jpql?
If I have a complex where clause which varies only slightly between many queries, in SQL I would 开发者_Go百科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
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.
In JPA2, the combination of EntityManager and CriteriaQuery is very powerful:
http://java.sun.com/javaee/6/docs/api/javax/persistence/criteria/CriteriaQuery.html
http://java.sun.com/javaee/6/docs/api/javax/persistence/EntityManager.html#createQuery(javax.persistence.criteria.CriteriaQuery%29
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.
精彩评论