Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Many to One Mapping Select Query Example

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

In many to one relationship, when ever child object is loaded from the database then automatically the parent object will also be loaded from the database. Let us an example on selecting single child object with its parent object.

But remember, in many to one we can see 2 types of object loadings (selecting)

  • proxy
  • early loading

In many to one relationship, the lazy attribute (in mapping xml) values are either proxy or false.  If we write lazy=”false” then when ever child object is loading then immediately parent object will also be loading from the database.

The default value of lazy is proxy, means here when ever child object is loaded then parent object is not loaded immediately, but a proxy object will be loaded with it (logical object)

When ever the parent object is accessed then at that moment that parent object will be loaded from the database.

Note: In our program am not using lazy attribute in xml files so just go ahead and insert this lazy attribute and test the output on the eclipse console

Example on Many to One With select Query

files required

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




Many To One Select One Child Object With Parent

Many To One Select All Child Objects With Parents

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.


6 Responses to “Hibernate Many to One Mapping Select Query Example”
  1. ateyah says:

    can you help me?
    i have this exception
    Caused by: Invalid column name ‘Vdummy’.

  2. Java4s says:


    Its working fine from my end, did many to one insert worked ? and may i know your table details.

  3. karthick says:

    please show the db query that we can understand clearly….thnx in advance…

  4. raghava says:

    hi siva,
    you have used many to one in examples insert &select

    but syntax is varying some time its has “class” attribute some times only column why?

  5. Raghava says:

    siva, please put some light on Annotation,userdef Annotation how to create?them
    thanx in advance

  6. subhani says:

    Dear sir,
    please send me, hibernate relationships with annotations.


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