Skip to main content

Searches and Queries

  • Chapter
  • First Online:
Beginning Hibernate

Abstract

In the last chapter, we discussed how the Hibernate session is used to interact with the database. Some of the session's methods take query strings in their parameter lists or return Query objects. These methods are used to request arbitrary information from the database. In order to fully show how they're used, we must introduce the Hibernate Query Language (HQL), used to phrase these requests. As well as extracting information (with SELECT), HQL can be used to alter the information in the database (with INSERT, UPDATE, and DELETE). We cover all of this basic functionality in this chapter. Note: Hibernate's query facilities do not allow you to alter the database structure.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 44.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Lombok works by analyzing Java source code. It does not walk a hierarchy; while this would be very useful, there are some real technical challenges to implementation. While someday Lombok may indeed be able to handle object hierarchies as conveniently as it handles simple objects, as of this writing it doesn’t work well for object hierarchies, so we’re not going to use it in this chapter.

  2. 2.

    There are lots of ways to do this; we could use a technique we used earlier in the book and create a new SessionFactory for each test, or we could use dbUnit ( http://dbunit.sourceforge.net/ ). This just seems more direct.

  3. 3.

    If you’re using the JPA configuration, the property name is “hibernate.show_sql.

  4. 4.

    Well, debug or whatever equivalent your logging library might use…

  5. 5.

    In case you’re wondering, this was done in the runtime configuration before copying the logs for this chapter.

  6. 6.

    If you want to see them all and be amazed, see http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#hql . There are a lot of expressions available through JPA, and Hibernate itself adds quite a few.

  7. 7.

    See http://xkcd.com/327/ . You’re welcome.

  8. 8.

    It’s possible to triage queries yourself, of course; all you have to do is validate the query with a grammar to allow the expressions you want. In the real world, most of us can’t justify the time and effort this entails. Don’t do it. Use the facilities that Hibernate makes available to you.

  9. 9.

    This join results in a projection, thus the use of the Object arrays.

  10. 10.

    Our test code uses a bulk delete to clear out the data, including the use of executeUpdate(), as mentioned earlier.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2016 Joseph B. Ottinger, Jeff Linwood and Dave Minter

About this chapter

Cite this chapter

Ottinger, J.B., Linwood, J., Minter, D. (2016). Searches and Queries. In: Beginning Hibernate. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-2319-2_9

Download citation

Publish with us

Policies and ethics