Hibernate hello world example in eclipse

This is 2 of 8 parts of tutorial series

Tutorial Content:

After basic understanding of hibernate framework.We are ready to start working on hibernate In this post,we will configure hibernate in eclipse and write our first hibernate program.For configuring hibernate,there are some prerequisites which you need to have on your system.

  1. Download hibernate framework.(I am using here latest hibernate version 4.1.9)
  2. Download any database.(I am using here sql server 2005)
  3. Download JDBC driver for database(I have downloaded jdbc driver for sql server 2005)
  4. Eclipse ide
  5. JDK 1.5 or above

Now,In eclipse IDE,click on File->new

Click on other and then select java project


Click on next and  Write project name.I have writtern here “Hibernate4HelloWorldProject”

Click on finish and now our project is created
Create new folder “jars” under src folder so for that right click on project->new->folder

Write folder name as “jars”

click on finish and empy jar folder will be created in src folder.
Now we will add the hibernate 4 libraries to the project. Extract the “hibernate-release-4.1.9.Final” file if you have not extracted. Now go to the “hibernate-release-4.1.9.Final->lib->required” directory and then copy all the jar files (Ctrl+C) and paste on the jars directory (of our project) in the Eclipse IDE.
Also download jdbc driver for your database and copy that jar to jars
Note- location of above jar files may vary from versions to versions. So if you are using other versions than 4.1.9 then you need to find jars in that version.

Now add all the jars to “Java Build Path”. Right click on the “Hibernate4HelloWorldProject” in project explorer and then select properties. Then select “Java Build Path” –> Libraries and then click on the “Add JARs” button. And add all the libraries to Java Build Path.

Click on OK.
you are done with configuring hibernate in eclipse.

Now we will write our first hibernate application.For configuring hibernate in eclipse,please refer previous post.I am using SQL server 2005 as database.

We will make User_table table in database using hibernate.

We will create User.java for creating above table in database.

1.User.java(Entity)

An entity can be considered as a lightweight persistence domain object. An entity defines a table in a relational database and each instance of an entity corresponds to a row in that table. An entity refers to a logical collection of data that can be stored or retrieved as a whole.
Create a new package org.arpit.javapostsforlearning to hold the java files. Right click on the “src” folder and then select New –> Package. Then provide the package name as org.arpit.javapostsforlearning and click on the “Finish” button.
Create a new Java file User.java under the package org.arpit.javapostsforlearning and add the following code:

@Entity is used for making a persistent pojo class.For this java class,you want to create a table in database.
@Entity(name=”User_table”) specify that create a table named “User_table” in database

2.Hibernate configuration XML:

After configuring hibernate in eclipse,we need to configure “hibernate.cfg.xml” for database configuration and other related parameters.By default,hibernate searches for a configuration file in a project’s root directory.Create a file named “hibernate.cfg.xml” in src folder.

Need to specify  JDBC driver class.
 :specify JDBC URL to the database instance.
 ” >:Specify database username
” >:Specify database password
” >:This property makes Hibernate generate the appropriate SQL for the chosen database.
hibernate.connection.pool_size ” >:This property limits the number of connections waiting in the Hibernate database connection pool.
” >:If you specify this property to be true then all sql statement will be printed to console.
” >:It specify operation on your database schema.Whether to drop and recreate your database schema or update current schema.
 >:Here you need to specify all java classes for which you want to create a table in database.You need to specify all entity classes here.

 

3.Main class:

Create a class named “HibernateMain.java” in src->org.arpit.javapostsforlearning
 

As we have discussed in our previous post,we have to create SessionFactory instance in order to communicate with Database in Hibernate

Project structure:

 4.Run it:

When you will run this application.You will get following output.

After execution of above program,you can check User_table table in your database.

5.SQL output:

Source code:

click to begin
6.1 MB .zip

Join Our Newsletter

Subscribe to Awesome Java Content

42 Comments

  1. Charlie May 29, 2013
  2. Charlie May 30, 2013
    • Swathi July 22, 2013
      • Dinesh August 29, 2014
        • arpitmandliya August 29, 2014
  3. Rana Ranvijay Singh July 15, 2013
    • rana ranvijay singh July 15, 2013
      • mbird April 21, 2014
  4. rajesh August 6, 2013
  5. jafar August 6, 2013
    • Nasri June 17, 2014
  6. Felix August 24, 2013
  7. vikas shukla September 17, 2013
  8. lecaros September 30, 2013
    • tommy April 2, 2014
  9. Godfather October 18, 2013
  10. Guest October 21, 2013
  11. ayush October 21, 2013
  12. vinod October 23, 2013
  13. Abdelazeam December 11, 2013
  14. Yasir December 20, 2013
  15. Jawad January 3, 2014
  16. Danila March 15, 2014
  17. shivsinghgamil April 3, 2014
  18. Abhi April 14, 2014
    • mbird April 22, 2014
  19. mbird April 21, 2014
    • mbird April 22, 2014
  20. PhongNguyen May 9, 2014
  21. venkat June 4, 2014
    • sudhir February 3, 2015
  22. Nasri June 17, 2014
  23. vinod June 26, 2014
    • macsap November 18, 2014
  24. vinod June 27, 2014
    • obn November 3, 2014
  25. The Greek January 28, 2015
    • Venkat November 20, 2015
  26. hel February 7, 2015
  27. Thirunavukkarasu March 26, 2015
  28. Muhammad Sadiq June 9, 2015
  29. rahul123wagh August 3, 2015

Add Comment

Join Our Newsletter

Subscribe to Awesome Java Content