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.
Keywords
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 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.
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.
If you’re using the JPA configuration, the property name is “hibernate.show_sql.”
- 4.
Well, debug or whatever equivalent your logging library might use…
- 5.
In case you’re wondering, this was done in the runtime configuration before copying the logs for this chapter.
- 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.
See http://xkcd.com/327/ . You’re welcome.
- 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.
This join results in a projection, thus the use of the Object arrays.
- 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
Rights 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
DOI: https://doi.org/10.1007/978-1-4842-2319-2_9
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-2318-5
Online ISBN: 978-1-4842-2319-2
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books