Hibernate, lesson2 – Queries
In the last post I have showed you a very basic example of usage in Hibernate. In this post I would like to show you all the different ways of executing a query while using Hibernate. The examples in this post use the same example from the last post. 1. Using the session functions The session object itself gives you methods to persist objects to the DB. For example: This code will fetch a row from the DB with primary key=111 and convert it to Contact object. Which is the equivalent to this SQL code: Another example: This code will insert a new row to the table. In other words this is the equivalent to this SQL code: Another example: This code will update a row in the table where the primary key is 111. In other words this is the equivalent to this SQL code: But as you probably noticed, the possibilities here are vary narrow. But there are other and better ways creating queries in Hibernate. 2. Using a Query object with HQL HQL is a query language designed by Hibernate It resembles SQL but has more options to it. Example: This will return a single result. That Continue reading Hibernate, lesson2 – Queries