You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Hibernate Hello World Program (Hibernate Insert Query)

Hibernate » On May 26, 2011 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..

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

 

Product.java:

 

Product.hbm.xml:

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.

hibernate.cfg.xml:

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.

ClientForSave.java

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

Output Eclipse

In The Database

Note:

  • 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
download-source-code-hibernate-insert

 








By ( Java/J2EE Developer & Web Designer )
Founder of Java4s.com. You can contact me at sivateja [at] java4s.com


Comments

40 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

    Mydoubt

    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..

  2. 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

  3. 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!

  4. java4s says:

    Yeah vaseem,

    we are thinking the same,
    very soon we are going to launch one forum.

  5. 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.

  6. Java4s says:

    @soumen

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

  7. harish says:

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

    thanking you

  8. Madhu says:

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

    Thanks to Java4s Team.
    Madhu.chimata

  9. shiva says:

    Ya of course ! this site very useful for us

  10. Java4s says:

    @Harish

    We will provide for sure.

    @Madhu,@Shiva

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

  11. 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.

  12. 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.

  13. 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.

  14. Java4s says:

    @Anand

    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.

  15. sanjay says:

    hi sir i m getting dis message

    log4j:warn no appenders could be found for logger
    (Org.hibernat.cfg.Environment)

    so how to solve
    thanks

  16. Java4s says:

    @sanjay

    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 :-).

  17. saptak says:

    Sir in the code ClientForSave.java 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.

  18. Java4s says:

    @Saptak

    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.

  19. 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’

  20. Java4s says:

    @Nandy

    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.

    http://www.java4s.com/hibernate/hibernate-hello-world-program-in-eclipse/

  21. Nandy says:

    @java4s

    It worked sir…thank you so much…

  22. suresh says:

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

  23. Saptak says:

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

  24. Java4s says:

    @saptak

    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.

  25. Vishal says:

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

  26. Java4s says:

    @Vishal

    Please ask your questions in our forum

  27. suresh says:

    hi Sir,
    pls provide reverse engineering in hibernate?

  28. Chetan says:

    Good explanation very precise..:)

  29. Jsam says:

    @Java4s
    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…
    thanks.

  30. Arun says:

    Good tutorial :-)

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

  32. Bhanu says:

    Thanks very simple example easy to understand

  33. Creditto says:

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

  34. 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..:)

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

  36. Bala says:

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

  37. Raj says:

    Please tell me how to use data source in hibernate.

  38. sujith says:

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

  39. 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 ?

  40. vishwa says:

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
So, 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 Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.