Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Many to One Mapping Insert Query Example

Hibernate » On Jul 25, 2011 { 14 Comments } By Sivateja

Let us see how to achieve hibernate many to one mapping with insert query, just go through few points before we start the example

  • In the many to one relationship, the relationship is applied from child object to parent object, but in one to may parent object to child object right..! just remember
  • many to one is similar to one to many but with the little changes
  • If we want to apply many to one relationship between two pojo class objects then the following changes are required

In the child pojo class, create an additional property of type parent for storing the parent object in child object [ If you are confused just remember you will be able to understand in the example ], in the child pojo class mapping file we need to write <many-to-one name=””> for parent type property unlike <property name=””>

Example on Many to One With Insert Query

files required

  • Vendor [parent pojo]
  • Customer.java [child pojo]
  • OurLogic.java [our logic]
  • Vendor.hbm.xml
  • Customer.hbm.xml
  • hibernate.cfg.xml

Vendor.java

Customer.java

Vendor.hbm.xml

Customer.hbm.xml

hibernate.cfg.xml

OurLogic.java

Notes:

  •  In line numbers 28,34,40 we are adding parent to child object
  • In line number 44,45,46 we are saving all child objects,  but you know some thing, according to this code at line number 45, first child object will be saved with the parent,  at 45,46 just child (customer object) only will be saved as parent is saved already

 

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
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.

Comments

14 Responses to “Hibernate Many to One Mapping Insert Query Example”
  1. Vivek says:

    please tell me about the column you used in customer.hbm.xml.

    at

    http://www.java4s.com/hibernate/hibernate-many-to-one-mapping-insert-query-example/

    and in which table we need to create this column and with what type of constraints.

    i am stuck at that point.

    Thanks

  2. Java4s says:

    @Vivek

    Vendor primary key will be stored in VDUMMY column of customer table.
    Output of above application will be llke

    SQL> select * from customer;

    CUSTID CUSTNAME VDUMMY
    504 customer4 101
    505 customer5 101
    506 customer6 101

    SQL> select * from vendor;

    VENDID VENDNAME
    101 java4s
  3. Anand says:

    Sir..In the many-to-one mapping you have missed the class name i.e. ‘str.Vendor'(in Customer.hbm.xml line no 11)
    http://www.java4s.com/hibernate/hibernate-many-to-one-mapping-insert-query-example/

    I think you should correct that…

  4. Java4s says:

    @Anand

    Yeah you are correct, that should contain class name too its corrected now. Thanks for the update.

    Every time we are facing this problem :-) my syntax script ignoring class attribute some times, however we have correct code in downloaded example, any time if you got this type of doubt please check with the downloaded one.

    Thanks lotz Anand.

  5. hi Java4s team,

    this is raju kapadne ,by reading this many to one concept , i got clearly many to one relationship thanks for that.

  6. Saleem says:

    Thank u for giving such a wonderful material …Thanks alot

  7. suvarna says:

    very nice tutorial……

  8. karthick says:

    why vdummy column of customer table contains the value of v.setVendorId(101)
    instead of v.setVendorName(“java4s”) value…can it store vendorname insted of vendorid
    pls clear my doubt sir….

  9. karthick says:

    should we define vdummy column as a foreign key column?
    pls clear my doubt…

  10. shrikanth reddy says:

    thanks a lot for such a easy and nice material…..

  11. shrikanth reddy says:

    how to get vname in customer table for that again we need to create extra coloum or what please clear my doubt…..

  12. Shyam Vaddepally says:

    why vdummy column of customer table contains the value of v.setVendorId(101)
    instead of v.setVendorName(“java4s”) value…can it store vendorname insted of vendorid
    pls clear my doubt sir….

    should we define vdummy column as a foreign key column?
    pls clear my doubt…

  13. pavan says:

    what is the main difference between one-to-many relationship and many-to-one relationship

    and at what situations it will be use.

  14. immaniyelu says:

    Sir, in many to one we took reference variable in child pojo class.can we take reference variable as collection type or not?

Name*
Mail*
Website



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
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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