hibernate

Introduction to Hibernate Query Language (HQL) with Hibernate 3

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.

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.

Your First Hibernate Program

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

Before starting, you should:

Downloading Hibernate 4 Jar Files

Hibernate 4 jars can be downloaded from the SourceForge web site at http://sourceforge.net/projects/hibernate/files/hibernate4.  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.2.4. Except for the version numbers, the jar names and folder structure should be same across any version of hibernate4 distribution.  

 

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.  

 

Modifying the Attributes of the Embedded Class in Hibernate 4.3

There might be situations where you would want to modify the attributes of the embedded class (eg. Address) within an embedding class (eg. User), for instance, changing the database column name for a field. You can do this within the embedded class itself. But what if you don’t have access to the embedded class or if you want different behavior for two different instances of the embedded class; you will have to override the embedded class behavior in the embedding class.

[Example-Lab] Modifying the Attributes of the Embedded Class from Embedding Class in Hibernate 4.3

There might be situations where you would want to modify the attributes of the embedded class (eg. Address) within an embedding class (eg. User), for instance, changing the database column name for a field.

We can use @AttributeOverride annotation to override the details of a field of the embedded class and then enclose all such @AttributeOverride annotations inside @AttributeOverrides annotation.

Eager and Lazy Fetch Types in Hibernate 4.3

When you load an embeding class, it may load its embedded collections either lazily or eagerly.

 

Important points about eager and lazy fetch types in Hibernate

  1. By default, when you load an embedding class (E.g. User or Company) using session.get,

    • its embedded collections (E.g. AddressList or EmployeeList) is not actually retrieved from database, but only the top level fields are retrieved.

Mappings in Hibernate 4.3

When referencing entities from other entities, we are mapping two entities whereas when referencing a value type we are embedding (or containing) a value type within an entity.

When mapping two entities, we annotate both classes as @Entity, and the mapping type is specified using the annotations @OneToOne, @OneToMany, @ManyToOne or @ManyToMany, over the reference variable declaration of an entity in another entity.

We will consider two entities for mapping examples – a student class and a course class.

[Example-Lab] Single Table Inheritance Strategy in Hibernate 4.3

In the SINGLE_TABLE strategy, hibernate will create a single table for all the classes in an inheritance hierarchy. There will be a discriminator column that tells us the class to which that row belongs.

In this example, we will create and save three entity classes – Shape, Rectangle and Circle; where Rectangle and Circle extends from Shape.

Pages

Search the Web

Custom Search

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