Get all updates of Java4s
Newsletter

Hibernate One To Many Annotation Example

Hibernate »on Aug 30, 2011 { 8 Comments } By Sivateja

Let us see an example on one to many annotations mapping…

Files required..

  • Customers.java
  • Vendor.java
  • ForOurLogic.java
  • hibernate.cfg.xml

Customers.java

Vendor.java

hibernate.cfg.xml

ForOurLogic.java

Output

 

You Might Also Like

Comments

8 Responses to “Hibernate One To Many Annotation Example”
  1. raju says:

    Hi Admin,

    Your code is executing perfectly,But i think is not right way.B’coz if the operation faild to insert values into chaild table then the insertion of parent table also should be rollback.But with this code i can’t get such outcome.(If i am wrong please correct me.)

    Thaks & Regords
    raju.

  2. Configuration cfg = new Configuration();
    cfg.configure(“hibernate.cfg.xml”);

    this is not required.
    Pls correct it..

  3. sasi says:

    Configuration cfg = new Configuration();
    cfg.configure(“hibernate.cfg.xml”);

    SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();

    so we have to write Configuration and AnnotationConfiguration??

  4. Mohan says:

    Configuration cfg = new Configuration();
    cfg.configure(“hibernate.cfg.xml”);

    The above code not required.

    • shantanu Surve says:

      Configuration cfg=new Configuration();
      cfg.configure("hibernate.cfg.xml");
      this code works good in hibernate version 4. But will not work till hibernate version 3. For that we
      need AnnotationConfiguration class i.e.
      Configuration cfg=new AnnotationConfiguration();
      cfg.configure("hibernate.cfg.xml");

      this code is required. AnnotationConfiguration this class is deprecated from version 4. So for compatibility for both the version he has wrote both the codes.

  5. Ashoka says:

    hii…..!

    u r just providing example….and not providing any explanation regarding @OneToMany annnotation and its attributes….!

  6. radheshyam says:

    perfect…add for retrieval also..!!!!!!!

  7. aman says:

    Exception in thread "main" org.hibernate.MappingException: <mapping> element in configuration specifies no attributes
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1326)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1285)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1267)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1234)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1162)
    at com.str.ForOurLogic.main(ForOurLogic.java:18)

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
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.