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]
  • Customer.java [child pojo]
  • OurLogic.java [our logic]
  • Vendor.hbm.xml
  • Customer.hbm.xml
  • hibernate.cfg.xml

Vendor.java

package str;

public class Vendor {

	private int vendorId;
	private String vendorName;

	public int getVendorId() {
		return vendorId;
	}
	public void setVendorId(int vendorId) {
		this.vendorId = vendorId;
	}
	public String getVendorName() {
		return vendorName;
	}
	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	}	

}

Customer.java

package str;
public class Customer {

	private int customerId;
	private String customerName;
	private Vendor parentObjects;

	public Vendor getParentObjects() {
		return parentObjects;
	}
	public void setParentObjects(Vendor parentObjects) {
		this.parentObjects = parentObjects;
	}
	public int getCustomerId() {
		return customerId;
	}
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
}

Vendor.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Vendor" table="vendor">

<id name="vendorId" column="vendid"  />
<property name="vendorName" column="vendname" length="10"/>

</class>
</hibernate-mapping>

Customer.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Customer" table="customer">

<id name="customerId" column="custid"  />
<property name="customerName" column="custname" length="10"/>
<many-to-one name="parentObjects" column="Vdummy" cascade="all" />

</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="Customer.hbm.xml"></mapping>
<mapping resource="Vendor.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

Many To One Select One Child Object With Parent

OurLogic.java

package str;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class OurLogic {

	public static void main(String args[])
	{

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();		

		Object o = session.get(Customer.class, new Integer(506));
		Customer c = (Customer)o;

		System.out.println(c.getCustomerName());
		Vendor v=c.getParentObjects();
		System.out.println(v.getVendorName()); 

		    session.close();
		    System.out.println("many to one select is done..!!");
		    factory.close();       

	}
}

Many To One Select All Child Objects With Parents

OurLogic.java

package str;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class OurLogic {

	public static void main(String args[])
	{

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();		

	    Query qry=session.createQuery("from Customer c");

	    List l=qry.list();
	    Iterator it = l.iterator();
	    while(it.hasNext())
	    {
	    	Object o = it.next();
	    	Customer c = (Customer)o;
	    	System.out.println(c.getCustomerName());
	    	Vendor v=c.getParentObjects();
	    	System.out.println(v.getVendorName());
	    }

	    session.close();
		    System.out.println("many to one select done..!!");
		    factory.close();       

	}
}

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

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

    can you help me?
    i have this exception
    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name ‘Vdummy’.

  2. Java4s says:

    @ateyah

    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 also..so that we can understand clearly….thnx in advance…

  4. raghava says:

    hi siva,
    you have used many to one in examples insert &select
    https://www.java4s.com/hibernate/hibernate-many-to-one-mapping-select-query/
    https://www.java4s.com/hibernate/hibernate-many-to-one-mapping-insert-query-example/

    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.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

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