Javatpoint Logo
Javatpoint Logo

First Hibernate Example without IDE

Here, we are going to create the first hibernate application without IDE. For creating the first hibernate application, we need to follow the following steps:

  1. Create the Persistent class
  2. Create the mapping file for Persistent class
  3. Create the Configuration file
  4. Create the class that retrieves or stores the persistent object
  5. Load the jar file
  6. Run the first hibernate application by using command prompt

1) Create the Persistent class

A simple Persistent class should follow some rules:

  • A no-arg constructor: It is recommended that you have a default constructor at least package visibility so that hibernate can create the instance of the Persistent class by newInstance() method.
  • Provide an identifier property: It is better to assign an attribute as id. This attribute behaves as a primary key in database.
  • Declare getter and setter methods: The Hibernate recognizes the method by getter and setter method names by default.
  • Prefer non-final class: Hibernate uses the concept of proxies, that depends on the persistent class. The application programmer will not be able to use proxies for lazy association fetching.

Let's create the simple Persistent class:

2) Create the mapping file for Persistent class

The mapping file name conventionally, should be class_name.hbm.xml. There are many elements of the mapping file.

  • hibernate-mapping : It is the root element in the mapping file that contains all the mapping elements.
  • class : It is the sub-element of the hibernate-mapping element. It specifies the Persistent class.
  • id : It is the subelement of class. It specifies the primary key attribute in the class.
  • generator : It is the sub-element of id. It is used to generate the primary key. There are many generator classes such as assigned, increment, hilo, sequence, native etc. We will learn all the generator classes later.
  • property : It is the sub-element of class that specifies the property name of the Persistent class.

Let's see the mapping file for the Employee class:


3) Create the Configuration file

The configuration file contains information about the database and mapping file. Conventionally, its name should be hibernate.cfg.xml .


4) Create the class that retrieves or stores the object

In this class, we are simply storing the employee object to the database.

5) Load the jar file

For successfully running the hibernate application, you should have the hibernate5.jar file.

Download the required jar files for hibernate

6) How to run the first hibernate application without IDE

We may run this hibernate application by IDE (e.g. Eclipse, Myeclipse, Netbeans etc.) or without IDE. We will learn about creating hibernate application in Eclipse IDE in next chapter.

To run the hibernate application without IDE:

  • Install the oracle10g for this example.
  • Load the jar files for hibernate. (One of the way to load the jar file is copy all the jar files under the JRE/lib/ext folder). It is better to put these jar files inside the public and private JRE both.
  • Now, run the StoreData class by java com.javatpoint.mypackage.StoreData
steps to create first hibernate application

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA