Using Hibernate Criteria framework for complex queries vs. just making views in DB

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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"))
</code>
<code>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")) </code>
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?

3

The main benefit of using the Criteria framework is programmatic query creation, and as Aaronaught implies, it’s much easier with Criteria to run an arbitrarily complex query on many different RDBMSs than it is with a named view that would need to be created and maintained separately.

Depending on your situation (and for your example), a view may be more maintainable and easily optimized. HQL would certainly be easier to read.

0

From the database perspective, you are more likely to be able to write a better performing view. You may be able to layer views together, where some of the views contain distinct, reusable chunks of business logic. When approached this modular way you greatly reduce the probability of having outrageous numbers of views. And this should simplify things in the “thousands” of Java applications using them, reducing development, maintenance, and performance costs on the Java side.

This may work well in some environments where you are dealing with a single DBMS. This also lays the groundwork for use outside you java development group. There might be applications wanting to use other access methods, now or in the future.

Of course some DBMS’s may not optimize the layered views approach as well as mine. YMMV.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật