Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Hello World Program (Hibernate Insert Query)

Hibernate » On May 26, 2011 { 62 Comments } By Sivateja

Mates, here is the first program in hibernate like saving an object into the database (don’t think we are inserting a record into the database :-) that is the case in JDBC, in hibernate we are just saving an object into database, means inserting only) hope you got my contention,  as of now am giving this as normal console based java application, actually it’s bit tedious to set the class path every time for all the jar files but you must know this too.

From the next example i will give all the applications in the Eclipse

As i told you earlier,  these are the files we require to shape an hibernate program..

  • (My POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • (java file to write our hibernate logic)



In this mapping file, my Product class is linked with PRODUCTS table in the database, and next is the id element, means in the database table what column we need to take as primary key column, that property name we need to give here, actually i have been given my property name productId which will mapped with pid column in the table.
And proName is mapped with pname column of the PRODUCTS table, see i have not specified any column for the property price, this means that, our property name in the pojo class and the column name in the table both are same.

Remember: the first 3 lines is the DTD for the mapping file, as a programmer no need to remember but we need to be very careful while you are copying this DTD, program may not  be executed if you write DTD wrong, actually we have separate DTD’s for Mapping xml and Configuration xml files.


In this configuration file i have been given my Oracle database connection properties, if you are using MySql then just specify your database related details actually its depends on you.

Now compile all .java files and run and check the output

Output Eclipse

In The Database


  • Make sure all .class, .java, .xml files are exist in the same folder
  • Before you compile and run this application, ensure you set the class path for all 12 jars files, this is tedious like what i told you earlier, we can avoid this process from the next example with Eclipse, a real time tool ;)
  • except select operation, all other operations must be in the Transaction Scope


You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


62 Responses to “Hibernate Hello World Program (Hibernate Insert Query)”
  1. Mohammed vaseem says:

    Hello java4s team,
    I modified your programs by just adding System.out.println statements to analyze the control flow.. Am getting doubt in that. please help


    1. why setter method of getproductid is called after getter method of getproductid
    2. why getter are methods are called from the lines “before commit and after commit” in the console as they are already get na?

    Thank you seeing this post..

    • java4s says:

      Hi vaseem,

      > Actually the priority will be, getter methods will be called only after setter methods.
      > Before commit always getter methods will be called because, there is a chance to modify the values before commit ?

      Hope you understand.
      We are requesting you to send lengthy text messages to our mail id rather writing here

  2. Mohammed vaseem says:

    Hello java4s Team,
    Hats off to Tremendous service of yours……….
    I request you to maintain a forum where all users can post their questions, can communicate with other users also in explaining queries. It will be very much flexible to learn the subject when it is shared, communicated with all…

    Thank You!

  3. soumen says:

    It’s a great website for beginners and professionals too. It quite helpful both in terms of concepts and examples. Kindly put a back and forth or next link in the detail tutorial page to next related article.

  4. Java4s says:


    Thank you soumen, yeah we are keep getting this suggestion of back-forth buttons from the users, we will implement this very soon.

  5. harish says:

    please provide how to implement singleton pattern in hibernate
    hope u provide this.

    thanking you

  6. Madhu says:

    It is a Best Suitable Tutorial for Biginners. I got exact flow of applications like Hibernate and Struts.

    Thanks to Java4s Team.

  7. shiva says:

    Ya of course ! this site very useful for us

  8. Java4s says:


    We will provide for sure.


    Thanks for your feedback, feel free to share this article, will be useful to all java freshers.

  9. prasanta kumar routray says:

    can u just tell to me .where i put cfg.xml and hbm.xml in eclipse.can i put on src folder.

  10. Java4s says:

    @prasanta kumar routray

    yeah src folder is fine, please check the next article(Hibernate Hello World Program in Eclipse) still if you have any doubt.

  11. Anand says:

    Sir i Want to know that why we put configure file out from the package(java).Can we put it with java files…please tell me about it..Thank you Sir.

  12. Java4s says:


    Configuration files must be in the folder contains all your classes files, just play by removing and adding the configuration files here to there :-) so that you will be able to understand exactly whats going, hope you understand.

  13. sanjay says:

    hi sir i m getting dis message

    log4j:warn no appenders could be found for logger

    so how to solve

  14. Java4s says:


    Actually that warning is about logging, as of now we no need to care about that, even i have show that in the output screen too :-).

  15. saptak says:

    Sir in the code only 1 row is inserted is (101,iphone,25000) but result shows that there are 4 rows inserted in the database. How to insert multiple rows? by creating multiple referrence of Product class?
    Pls Pls reply Sir.

  16. Java4s says:


    Bulk you can do in future concepts.

    As of now you can work only with single object only. As you are fresher you need to know some more concepts :-), move one by one so that you will be able to understand well.

  17. Nandy says:

    hello sir,these were the errors generated when i tried to run the program…

    1.Build path specifies execution environment JavaSE-1.6. There are no compatible JREs in the workspace.

    2.The project cannot be built until build path errors are resolved

    3.Unbound classpath container: ‘JRE System Library [JavaSE-1.6]’ in project ‘First_hyberPro’

  18. Java4s says:


    Seems you have different versions JDK,JRE’s, do one thing please uninstall java and reinstall (don’t let your java software update automatically).

    Once you installed configure Eclipse normally(with default settings) and start your first example by following these steps.

  19. Nandy says:


    It worked sir…thank you so much…

  20. suresh says:

    Hi sir,
    explain hibernate tools with procedure(screen shots).
    is it possible to develop hibernate application without configuration file?(pls provide example)

  21. Saptak says:

    Sir I getting this error when running the application that Product.hbm.xml not found.
    (no html)

  22. Java4s says:


    Make sure you have added Product.hbm.xml in your configuration file and in your application folder as well in appropriate position, download the application and check if you still have any issues.

  23. Vishal says:

    What is Hibernate N+1 problem. please give me answer with an example

  24. Java4s says:


    Please ask your questions in our forum

  25. suresh says:

    hi Sir,
    pls provide reverse engineering in hibernate?

  26. Chetan says:

    Good explanation very precise..:)

  27. Jsam says:

    Thanks a lot. very useful for freshers. As i m a beginner it helps me a lot… it helped me to make a first step in hibernate…

  28. Nice one, very good. Thanks a lot this article. It helped me a lot.

  29. Bhanu says:

    Thanks very simple example easy to understand

  30. Creditto says:

    Hai , Thanks a lot . it helped me to make a first step in hibernate…

  31. Hello sir ,i am Sachin and fresher in java.I learned PHP.and i thought that JAVA is a tough language.But when i visited your site i promise myself that i will definately learn java from Here.

    Thanks a lot for such a wonderful tutorials..:)

  32. Hi thank you so much….i learnt a lot from this site..

  33. Bala says:

    Wow…!!! it’s really grate….very very helpfull thanks a lot….

  34. Raj says:

    Please tell me how to use data source in hibernate.

  35. sujith says:

    i got this program…… really good…thank you………

  36. Subhankar says:

    Hello Sir,

    I am beginner in Hibernate Frame work. I want to create Mysql Database Schema (new database) automatically. But its not working showing error that unknown database. How to solved this problem.
    I use create/update that work only for table creation/update.
    How to create new DB Schema rather than new table ?

  37. vishwa says:

    hello sir what is singleton design pattern when will we use in project give me any real time example???

  38. Murali says:

    Hello Sir,
    My I know the high level program of hibernate which we used in all real time applications commonly.
    thanks in Advance..but please give me reply either by mail Id or as a reply to this comment

  39. suneel says:

    hi,am new to the practise in eclipse,i follow your steps whatever you mentioned in this getting the below error.tell me the reasons & steps come out for this problem
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread “main” org.hibernate.HibernateException: hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(
    at org.hibernate.cfg.Configuration.configure(
    at str.Client.main(

  40. Arun says:

    It works perfectly. Thanks alot.
    Very nice tutorial.

  41. Engineer says:

    For boilderplate code, we move to spring framework.
    To manage the classpath for 12 jar files, we move to eclipse.
    May be to move to maven for managing the dependency graph of jars?

    When did we started down this path of covering up engineer’s ignorance, lazyness, resistence to learn fundamentals with increasingly bigger and complex frameworks?

    Factories, sessions, … Every new question you get with plain SQL, JDBC, now needs to be rephrased, reanswered. For ex., what do I do if I have the table with same name but my application’s runtime configuration is going to decide if I am going to store the object in Oracle or DB2? I know, there is an answer for it. But, is it obvious? It needs to be understood now in “Hibernate’s Lingo”.

    In order to move a mosquito, now we are proposing a nuclear factory

  42. jeya says:

    if i run my project it may show a hibernate which is referenced by a class path does not exist error

  43. Madhuri says:

    Hi , i am getting below error while running my code..
    Can anyone please help me in this..

    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.configure(
    Caused by: org.dom4j.DocumentException: Nested exception:
    at org.hibernate.cfg.Configuration.doConfigure(
    … 2 more

  44. Swamy Gunti says:

    tutorial is good but you explained how to save one object only,
    but i want to know multiple objects at a time with what we give input with key board at runtime….

  45. Nandha kumar says:

    This site is too useful. contents are so simple to understand.

    Nandha kumar C

  46. Revathi says:

    I know 12 jar files require for hibernate… but i don’t know how to set jar files in eclipse can you please help me…

  47. anbarasu says:

    After long day of tried in eclipse,finally ran in Netbeans. thx lot.

  48. Suresh says:

    Please help me,

    when I run in apache tomcat folder

    javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/dom4j/DocumentException

  49. Kabir khan says:

    Hello sir,

    I am beginner. How can I provide a message data store in database or not. How to conform data store in database. In jdbc return 0 or 1 to know data save successfully.

  50. Venkateswarlu Koppu says:

    Java4s is the best tutorial,
    I got good knowledge in hibernate through the java4s.

  51. Shwetha R says:


    I started to learn hibernate and am when am trying this sample program, object is not saved in database. is not working. Can anyone help me. Connection has happened table is created but object is not saved. How to solve this problem.

  52. Ashis says:


    It Should Be instead of (Line no. 19 of hibernate.cfg.xml).

  53. Amol Unde says:

    very good tutorial.but require NEXT button on website.

  54. Rajanikanta Pradhan says:

    Awesome tutorials easy to understand.

  55. Munisekhar says:

    Hi sir,

    my question is??

    what is the difference between satatic and transient in the process of serilization ,i want difference between them….

  56. ajit says:

    Hi ,

    This is very good site for beginners.Thanks a lot for your valueable time.

    in above program,hibernate.cfg.xml file u have mentioned dialet instead of dialect due to which getting error “Hibernate Dialect must be explicitly set”

  57. mahendra says:

    Hi java4s team,

    I executed program in eclipse but i got one exception. The exception i mention bellow.

    Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.configure(
    at san.StoreData.main(
    Caused by: org.dom4j.DocumentException: Error on line 3 of document : The document type declaration for root element type “hibernate-configuration” must end with ‘>’. Nested exception: The document type declaration for root element type “hibernate-configuration” must end with ‘>’.
    at org.hibernate.cfg.Configuration.doConfigure(
    … 2 more

    Please give me replay as soon as possible.

    thank you

  58. praphulla says:

    Hello Sir,
    I have loaded the postgres driver for my Hibernate application but not able to connect to Database it is showing following error ,please help me..
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(
    at org.hibernate.exception.JDBCExceptionHelper.convert(

  59. jack says:

    Could not find or load main class antlr.debug.misc.ASTFrame


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2017 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.