Hibernate is an Object-Relational Mapping(ORM) solution and a powerful, high performance Object-Relational Persistence and Query service for any Java Application. It maps Java classes to database tables and from Java data types to SQL data types.
- Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code.
- Provides simple APIs for storing and retrieving Java objects directly to and from the database.
- If there is change in Database or in any table then the only need to change XML file properties.
- Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
- Hibernate does not require an application server to operate.
- Manipulates Complex associations of objects of your database.
- Minimize database access with smart fetching strategies.
- Provides Simple querying of data.
Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.
- HSQL Database Engine
- Microsoft SQL Server Database
- Sybase SQL Server
- Informix Dynamic Server
Read more: http://www.tutorialspoint.com/hibernate/
Hibernate requires to know in advance where to find the mapping information that defines how your Java classes relate to the database tables. Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.
Hibernate Persistent Class
The entire concept of Hibernate is to take the values from Java class attributes and persist them to a database table. A mapping document helps Hibernate in determining how to pull the values from the classes and map them with table and associated fields.
Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the Plain Old Java Object (POJO) programming model.
Hibernate Mapping Example
An Object/relational mappings are usually defined in an XML document. This mapping file instructs Hibernate how to map the defined class or classes to the database tables.
Though many Hibernate users choose to write the XML by hand, a number of tools exist to generate the mapping document. These include XDoclet, Middlegen and AndroMDA for advanced Hibernate users.
There would be one table corresponding to each object you are willing to provide persistence. Consider above objects need to be stored and retrieved into the following RDBMS table:
Based on the two above entities we can define following mapping file which instructs Hibernate how to map the defined class or classes to the database tables.
You should save the mapping document in a file with the format .hbm.xml.
So far you have seen how Hibernate uses XML mapping file for the transformation of data from POJO to database tables and vice versa. Hibernate annotations is the newest way to define mappings without a use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata.
Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. All the metadata is clubbed into the POJO java file along with the code this helps the user to understand the table structure and POJO simultaneously during the development.
If you going to make your application portable to other EJB 3 compliant ORM applications, you must use annotations to represent the mapping information but still if you want greater flexibility then you should go with XML-based mappings.
Firstly, you need to upgrade your Hibernate JDK to JDK 5.0 to take advantage of the native support for annotations.Second, you will need to install the Hibernate 3.x annotations distribution package, available from the sourceforge: (Download Hibernate Annotation) and copy hibernate-annotations.jar, lib/hibernate-comons-annotations.jar and lib/ejb3-persistence.jar from the Hibernate Annotations distribution to your CLASSPATH
Following is the mapping of Employee class with annotations to map objects with the
Hibernate detects that the @Id annotation is on a field and assumes that it should access properties on an object directly through fields at runtime. If you placed the @Id annotation on the getId() method, you would enable access to properties through getter and setter methods by default. Hence, all other annotations are also placed on either fields or getter methods, following the selected strategy.
- The @Entity annotation marks this class as an entity bean, so it must have a no-argument constructor that is visible with at least protected scope.
- The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database.
- The @Table annotation provides four attributes, allowing you to override the name of the table, its catalogue, and its schema, and enforce unique constraints on columns in the table. For now we are using just table name which is EMPLOYEE.
- Each entity bean will have a primary key, which you annotate on the class with the @Id annotation. The primary key can be a single field or a combination of multiple fields depending on your table structure.
- By default, the @Id annotation will automatically determine the most appropriate primary key generation strategy to be used but you can override this by applying the @GeneratedValue annotation which takes two parameters strategy and generator. Letting Hibernate determine which generator type to use makes your code portable between different databases.
- The @Column annotation is used to specify the details of the column to which a field or property will be mapped. You can use column annotation with the following most commonly used attributes:
- name attribute permits the name of the column to be explicitly specified.
- length attribute permits the size of the column used to map a value particularly for a String value.
- nullable attribute permits the column to be marked NOT NULL when the schema is generated.
- unique attribute permits the column to be marked as containing only unique values.
What is hibernate NaturalId?
In Hibernate, natural keys are often used for lookups. You will have an auto-generated surrogate id in most cases. But this id is rather useless for lookups, as you’ll always query by fields like name, social security number or anything else from the real world.
When using Hibernate’s caching features, this difference is very important: If the cache is indexed by your primary key (surrogate id), there won’t be any performance gain on lookups. That’s why you can define a set of fields that you are going to query the database with – the natural id. Hibernate can then index the data by your natural key and improve the lookup performance.
Create Application Class:
Finally, we will create our application class with the main() method to run the application. We will use this application to save few Employee’s records and then we will apply CRUD operations on those records.
Now let us create hibernate.cfg.xml configuration file to define database related parameters.