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

Hibernate Hello World Program in Eclipse

Hibernate » On Jun 9, 2011 By Sivateja

Mates, now am going to show like how to execute the previous hibernate program in Eclipse IDE to make our world little easy. You might be fresher or not aware of executing java programs in the eclipse what ever just follow these setps…..

1. Download Eclipse IDE for Java EE Developers
2.
Unzip and open Eclipse
3. Now create a new java project like, File –> New –> Java Project

4. See now it will opens one window, there give the project details like project name and keep the other details as it is.. and press Finish

5. so you have been created one new java project and at the left side you must see like.. (Before this, make sure you have been selected navigator view in windows –> show view –> Navigator)

6. create the following files in the src folder..

  • In the src create sub folder with name  str(like package) , and create 2 xml files with names hibernate.cfg.xml and Product.hbm.xml
  • In the str folder create 2 more java files with names, Product.java and ClientForSave.java
  • so the final directory structure will be..

Actually our work is to do every thing in src folder and Eclipse will automatically create  and copies all the related .classes files, folders and .xmls in bin folder, what am saying is as a programmer we no need to touch bin folder, ours is src only :-)

7. Now copy and paste the code into the files in the eclipse..
8. Finally double click on ClientForSave.java so that file will be opens, then right click on the code and Run As –> Java Application

9. Now see the console.., it will be like

That’s it…, you are done..!!

And finally in the database

And mates, this is the same process for the hibernate applications, actually its little time taking for me to give all the examples in the Eclipse so from the next example am going to give related files only, you can carry on with Ecliplse  ;)

 







By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja@java4s.com


Comments

16 Responses to “Hibernate Hello World Program in Eclipse”
  1. ramya says:

    this is very nice tutorials for beginners,….it helped me a lot….thanks

  2. Surendra Kumar says:

    When i run this code i got such type of error how can i resolve this error
    Configuration cannot be resolved to a type
    Configuration cannot be resolved to a type
    SessionFactory cannot be resolved to a type
    Session cannot be resolved to a type
    Transaction cannot be resolved to a type

  3. Java4s says:

    @ramya

    You welcome :-) glad to hear that its helped you.

  4. Java4s says:

    @Surendra Kumar

    Hi seems you are not added .jar files in your class path.

    Do one thing..

    Right click on your project –> Build Path –> Configure Build Path_

    Add your hibernate jar files there, hope you will be fine with this.

  5. Pavan says:

    Hi Sir,i am beginner in hibernate and great tutorials really love to study.Can u tell me sir before running the hibernate program do i need to create the table structure in the database or table creation is done by itself internally.

  6. Java4s says:

    @Pavan

    Actually thing is, in hibernate.cfg.xml if we set hbm2ddl.auto property value to update, then hibernate will automatically update the table in the database, and creates the table if not exist in the database.

    You will see about this in future articles, for now just remember as you are fresher :-)

  7. sample java says:

    Hi sir,

    you are doing all coding in eclipse, I need to know how about netbeans

    Thanks

  8. Davis says:

    “The import org.hibernate cannot be resolved” this is what i see in ClientForSave.java file. what to do?

  9. Java4s says:

    @Davis

    Make sure you set Jar files in your class path, before you start.

  10. Jsam says:

    @Java4s
    Thanks a lot. as i m a beginner it helps me a lot… it helped me to make a first step in hibernate… when i master the hibernate , that credit goes to you :-)

    thanks.

  11. saikiran says:

    instead of saying sub folder like package in src , say as source folder. thanks done with my first hibernate project..kudos for ur effort

  12. Srinivas N says:

    I tried to create a sample program using hibernate but getting below error.

    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(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at com.srinivas.hibernate.StoreData.main(StoreData.java:14)
    Caused by: org.dom4j.DocumentException: Error on line 1 of document : A pseudo attribute name is expected. Nested exception: A pseudo attribute name is expected.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    … 2 more

    Please help me, and redirect your replies to my email nudurupatisrinivas@gmail.com TIA.

  13. Exception in thread “main” java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    at str.ClientForSave.main(ClientForSave.java:11)
    Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:354)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:353)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    … 1 more
    What’s the Cause of the error?

  14. Exception in thread “main” java.lang.Error: Unresolved compilation problems:
    Configuration cannot be resolved to a type
    Configuration cannot be resolved to a type
    SessionFactory cannot be resolved to a type
    Session cannot be resolved to a type
    Product cannot be resolved to a type
    Product cannot be resolved to a type
    The method commit() is undefined for the type Transaction

    at str.ClientForSave.main(ClientForSave.java:9)

    How do i add the jar files please let me know the steps

  15. Vaishali says:

    best tutorial i have ever seen….

  16. suv says:

    I have put the components in the same folder. But I am getting the error Exception in thread “main” org.hibernate.MappingException: entity class not found: Employee. Please help

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.