Skip to main content
Top

2016 | OriginalPaper | Chapter

10. Advanced Queries Using Criteria

Authors : Joseph B. Ottinger, Jeff Linwood, Dave Minter

Published in: Beginning Hibernate

Publisher: Apress

Activate our intelligent search to find suitable subject content or patents.

search-config
loading …

Abstract

Hibernate provides three different ways to retrieve data. We have already discussed HQL and the use of native SQL queries; now we add criteria.

Dont have a licence yet? Then find out more about our products and how to get one now:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Footnotes
2
Astute readers will notice that Chapter 9 didn’t actually do much Session management at all; before the test methods ran, a Session was created and stored in an instance-level attribute. That’s not a very nice way to do Session management; this is much better. Plus, lambdas are fun.
 
3
Strictly speaking, you can build a metamodel yourself. However, it should be kept in sync with the entity classes themselves, and it’s certainly eligible for generation – and if we can get the computer to maintain the metamodel, we should.
 
4
A Predicate is a functional interface that has a single method – test() – that determines fitness for a match, so it serves the same purpose as our Expression<Boolean> would.
 
5
If the query expression were “%mOUse” – as in the next example – it wouldn’t match “Mouse” because the cases of the content do not match. Since user input tends to not conform to what database developers would like, it’s probably wise to sanitize this somewhat.
 
6
We are apparently feeling rather thrifty or we’re trying to throw money around, apparently.
 
7
When naïve pagination would not lead to atrocious performance, chances are the result set is small enough that you wouldn’t need pagination in the first place.
 
8
Thus, it’s about a unique result and not just a single result. You have to enforce uniqueness if it’s not already present. If we’d called setMaxResults (1) in the testUniqueResultWithException() test, no exception would have been thrown.
 
9
It is a steep learning curve; we’ve only scratched the surface of the Criteria API. There is a lot to the API, and it could easily fill a book all by itself.
 
Metadata
Title
Advanced Queries Using Criteria
Authors
Joseph B. Ottinger
Jeff Linwood
Dave Minter
Copyright Year
2016
Publisher
Apress
DOI
https://doi.org/10.1007/978-1-4842-2319-2_10

Premium Partner