I like using Hibernate
for regular simple CRUD
operations.
However, I am trying to understand why anyone would resort to its Criteria
framework to assemble complex recordset criteria as opposed to simply creating a view at the database level.
E.g. here is a paraphrased snippet I am working with:
Criteria tableCriteria = session
.createCriteria(SomeEntity.class)
.add(Restrictions.eq("someID", someID))
.add(Restrictions.gtProperty("scoreOne", "scoreTwo"))
.add(Restrictions.gt("scoreThree", scoreThreeMin))
.add(Restrictions.or(Restrictions.eq("inactive", Boolean.FALSE), Restrictions.isNull("inactive"))).addOrder(Order.asc("ordCol"))
From the naked eye perspective, it is possible but let legible to discern what this filter definition is stipulating than to simply make a DB view where these restrictions could be expressed in the form of simpler WHERE
clauses.
Am I missing something? Is there an advantage to using Criteria vs. DB views that I am not aware of? Using views seems much cleaner. Is there a performance upside to Criteria?
InactiveCustomersWithAtLeastOneOrderByTransactionYearOrderedByServiceTierAndNumberOfPurchases
without going insane?View
definitely is better.CriteriaQuery
can't handle complex joins and even it's not able to handle one query there's no question of portability. And overhead of learning CriteriaQuery, unit testing each service method and than converting it toDTO
. WithView
you can have just an entity defined and directly pass theEntity
instead ofDTO
since it will or should only contain the columns you need.changeSets
as always run and havedrop
andcreate
view statements. So they are updated with each change in the base table.