Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Hello World Program With Annotations

Folks we will see one simple program with hibernate annotations, let us take inserting a record [ saving one object ] into the database application.  And remember in the annotations no need to write mapping xml, hope you remember the previous sessions :-)

Files required..

  • [ our pojo class ]
  • hibernate.cfg.xml


And friends,  no need to explain there i think.  Please refer previous introduction sessions on annotations in case  you have any doubt.


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.


11 Responses to “Hibernate Hello World Program With Annotations”
  1. Mohammed Vaseem says:

    Hello Java4s,
    Am getting exception in this program.
    Showing exception as
    Exception in thread “main” org.hibernate.MappingException: Unknown entity: str.Product

    Please help!

  2. Mohammed Vaseem says:

    I got solution for the exception which i was facing.

    Exception in thread “main” org.hibernate.MappingException: Unknown entity: str.Product

    If we add the two lines in the hibernate.cfg file then i did not got that exception again and the program got executed successfully..

  3. Java4s says:


    Hmm… str.Product !
    Vaseem what are those 2 lines you added into hibernate.cfg ?

  4. Mohammed Vaseem says:

    The program can get executed even with one line..
    that line is..

    When we are not using hbm.xml file(mapping file), then we have to give fully qualified name of the annotated class to the mapping element.

  5. Ambresh Sharna says:

    i need some more emphasis on NamedQuery theory and Example like as development mode i can use that..if you can do… would b better for me…Thanks Java4s

  6. Aalam says:

    In the hibernate.cfg.xml file there is no mapping for pojo class…we need to map the pojo class as we are not using hbm.xml

  7. Java4s says:


    Yeah in the 18th line, we must specify the mapping class name you are correct, that should be class=”str.Product”. Corrected..!!

    Thanks Aalam :-)

  8. stephen says:

    Db table name : student_table
    Table name mentioned as @Table(name = “student_talbe”)

    table name mentioned in are differnt. How will this program work ? :(

  9. To stephen: Hi Stephen,Hibernate will create table automatically as per given query in “update” in hibernate.cfg.xml file.Here you can also use “create” instead of update.

  10. Govardhan MK says:

    Sir as you said we use annaotations to avoid using mapping xml i.e instead of using mapping xml we r using annotations.

    My question is in your herbernate.cfg.xml file Why you are again using

    please answer me!!!!!!

  11. Raja says:

    Hi,Java4s i have a solution for Hibernate Mapping Exception

    Solution is:

    Just set in your client class 15th line


    No need to set any
    <mapping class="str.Product"/> in your configrtation FIle


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