CRUD Operations in Hibernate 4.3

CRUD operations are Create(save), Read(select), Update(update) and Delete(delete).

Examples discussed here need to be tried out on top of the lab done @  http://javajee.com/lab-your-first-hibernate-43-program.



Select, Save, update and delete operations in hibernate should be called within a session:

 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

[Lab] Your First Hibernate 4.3 Program

We will write a simple hibernate application to get started with hibernate.

Before starting, you should:

Downloading Hibernate 4.3 Jar Files

Hibernate 4 jars can be downloaded from the SourceForge web site at http://sourceforge.net/projects/hibernate/files/hibernate4 or from http://hibernate.org/orm/downloads/.  If this url doesn’t work or get changed, just search ‘hibernate 4 jars download sourceforge’ in Google.  The distribution version downloaded here is 4.3.11. Except for the version numbers, the jar names and folder structure should be same across any version of hibernate4 distribution.  


Hibernate 4.3 Essentials

This note book will help you to get started with Hibernate 4 like setting up environment, inheritance, mappings, HQL and the Criterial API. 

All the examples in this book is tested with Hibernate version 4.3.11.

Please refer to the hibernate references section to see the reference books that we use for learning and training purposes.

Querying Using Criteria API in Hibernate

Hibernate provide Criteria Queries as an object-oriented alternative to HQL. Criteria Query is used to modify the objects and provide restriction for the objects. Many complex queries in HQL or even SQL may become larger over a period of time and spread over many lines; and will be difficult to update and maintain. We have already seen HQL in http://www.javajee.com/introduction-to-hibernate-query-language-hql-0.

Named Queries in HQL

We can have queries saved with a name and later we can retrieve them simply using that name. We use @NamedQuery annotation to declare a named query. We can also have a named query for native sql. For native SQL we use the annotation @NamedNativeQuery annotation. This is one of the important advantages of having a named query: you can write native SQL. You can retrieve a Query object from a saved query name (hql and native named query) using session.getNamedQuery method.

Parameter Binding in HQL

Parameter binding is the process of binding a Java variable with an HQL statement. Using Parameter binding and not string concatenation for HQL statement creation will also guard against attacks like SQL injection.

Introduction to Hibernate Query Language (HQL)

Hibernate provides a query language called Hibernate Query Language (HQL). HQL is similar to SQL in syntax, but HQL queries are written against Hibernate's entity objects, not database tables. Hibernate also provide Criteria Queries as an object-oriented alternative to HQL. Criteria Query is used to modify the objects and provide the restriction for the objects. Here we will see the basics of HQL and later in another tutorial we will see criteria queries.

Introduction to Hibernate Framework

Hibernate is a framework that help you easily map Java classes to database tables (and Java fields to DB columns, Java data types to DB data types etc.), without writing the usual create, select, update and delete sql queries.


Object Relational Mapping (ORM)

Hibernate is an ORM framework. Hibernate help you map your object model to a relational model. 

Joined Inheritance Strategy in Hibernate

Hibernate uses some strategies for saving an inheritance hierarchy of classes created in Java to the databases like SINGLE_TABLE, TABLE_PER_CLASS and JOINED.  We use @Inheritance annotation over the entity class to specify the inheritance strategy. We specify the inheritance strategy by assigning a strategy from the InheritanceType enumeration to the strategy parameter of the @Inheritance annotation.  Strategies defined in the InheritanceType enumeration are InheritanceType.SINGLE_TABLE, InheritanceType.TABLE_PER_CLASS and InheritanceType.JOINED.


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!