Book Image

Java Hibernate Cookbook

By : Yogesh Prajapati, Vishal Ranapariya
Book Image

Java Hibernate Cookbook

By: Yogesh Prajapati, Vishal Ranapariya

Overview of this book

This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application. We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.
Table of Contents (15 chapters)
Java Hibernate Cookbook
About the Authors
About the Reviewers


There are multiple databases available to store our valuable data. When we go for relational data structures, we can perform any operation on the data using queries on the interface provided by the database vendor or using a third party tool. The syntax for all databases is almost similar, but some databases follow their own syntax and semantics of writing a query. Nowadays, real-world applications require a quick development cycle, database-independent query execution, and a generic code that can be supported by multiple databases. It's a very hard and time-consuming task for a software developer to fulfill this set of requirements.

Hibernate is an ORM (Object Relational Mapping) tool that helps us by making our development process faster and making the software development independent of the database; so, we can easily change a database vendor without worrying about the changes required in code. Therefore, whether you are developing a standalone Java application or a server-side Java Enterprise application, you could use hibernate to make your code database-independent.

Java Hibernate Cookbook will help you to learn hibernate from the basics to an advanced level. In this book, we will try to create simple and short recipes to understand hibernate step by step.

What this book covers

Chapter 1, Setting Up Hibernate, provides the basics of hibernate and the persistent class. Next, you will learn to obtain the required libraries, XML and annotation-based configuration, and the mapping required for hibernate.

Chapter 2, Understanding the Fundamentals, takes you through the basic objects required to start working with hibernate, such as SessionFactory, Session, Criteria, Projection, and so on.

Chapter 3, Basic Annotations, covers the very basic annotations that are useful and necessary while writing with hibernate, such as declaring table (@Table), declaring column (@Column), declaring primary key (@Id), and so on.

Chapter 4, Working with Collections, explains how collections work with hibernate and how to persist Java collections such as List, Map, Set, and so on using hibernate.

Chapter 5, Working With Associations, helps you to understand relationships and associations such as one-to-one, one-to-many (many-to-one), and many-to-many. In this chapter, you will discover the simplest way to implement a relationship using hibernate.

Chapter 6, Querying, applies the basics of hibernate to query a database. This chapter helps you to understand the fundamentals of hibernate such as alias, subquery, NamedQuery, formula, and HQL.

Chapter 7, Advanced Concepts, helps you to learn the advanced concepts in hibernate such as caching, inheritance strategy, versioning, and maintaining the history of the objects.

Chapter 8, Integration with Other Frameworks, explains integration with other MVC frameworks such as Struts and Spring. It shows how to achieve a persistent life cycle in the frameworks.

What you need for this book

Knowledge of the Java programming language is a must. Also, you are expected to have an understanding of the relational database, SQL query, and JDBC API. The knowledge of IDEs such as Eclipse or NetBeans is preferred.

You will need the following software/tools:

  • JDK 6

  • Eclipse IDE

  • Maven

  • Hibernate 3.6.7 JAR

  • MySQL Database Server

  • JDBC Driver for MySQL

  • Hibernate Envers

Who this book is for

This is book for the Java developers who now want to learn hibernate. A good knowledge and understanding of Java is preferred to allow an efficient programming of the core elements and applications. It would also be helpful if the readers are familiar with the basics of SQL.


In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any other preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of any additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "dialect helps hibernate to generate database specific SQL statements."

A block of code is set as follows:

  <class="Employee" table="employee">
    <id name="id" type="long" column="id">
      <generator class="increment" />
    <property column="firstName" name="firstName" />
    <property column="salary" name="salary" />

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class Employee {
  // other fields and setters/getters


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.