Hibernate One to Many Select Query Example
|
Tweet
|
Let us see the logic for hibernate one to many mapping select query,
hibernate one to many select means, if you select the parent object then automatically its corresponding child objects will also be selected, see i have given for the logic for selecting single parent object with all its childs & all parent objects with all child objects
But mates, ensure you came through these sessions for better understand
Files required..
- Vendor.java [pojo class]
- Customer.java [pojo class]
- OurLogic.java
- Customer.hbm.xml
- hibernate.cfg.xml
- Vendor.hbm.xml
Vendor.java
package str;
import java.util.Set;
public class Vendor {
private int vendorId;
private String vendorName;
private Set children;
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;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}
Customer.java
package str;
public class Customer {
private int customerId;
private String customerName;
private int forevenId;
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;
}
public int getForevenId() {
return forevenId;
}
public void setForevenId(int forevenId) {
this.forevenId = forevenId;
}
}
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"/> <property name="forevenId" column="forevenid" insert="false" /> </class> </hibernate-mapping>
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"/> <set name="children" cascade="all" > <key column="forevenid" /> <one-to-many class="str.Customer" /> </set> </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>
Select Single Parent Object With All Child
OurLogic.java
package str;
import java.util.Iterator;
import java.util.Set;
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(Vendor.class, new Integer(100));
Vendor v=(Vendor)o;
System.out.println(v.getVendorId());
System.out.println(v.getVendorName());
Set s=v.getChildren();
Iterator it = s.iterator();
while(it.hasNext())
{
Object o1 = it.next();
Customer c = (Customer) o1;
System.out.println("---------------------------");
System.out.println("Customer objects...");
System.out.println("---------------------------");
System.out.println(c.getCustomerId());
System.out.println(c.getCustomerName());
System.out.println(c.getForevenId());
System.out.println("---------------------------");
}
session.close();
System.out.println("One To Many is Done for selecting.....!");
factory.close();
}
}
If we want to select all the parent objects will all its corresponding child objects., then the logic will be like..
Select All Parent Objects With All Childs
OurLogic.java
package str;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class OurLogicAllParents_AllChildrens {
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 Vendor v");
List l=qry.list();
Iterator it = l.iterator();
while(it.hasNext())
{
Object o = it.next();
Vendor v = (Vendor) o;
System.out.println(v.getVendorId()+ " " +v.getVendorName());
Set s= v.getChildren();
Iterator it1=s.iterator();
while(it1.hasNext())
{
Customer c = (Customer) it1.next();
System.out.println(c.getCustomerId()+" "+c.getCustomerName()+ " "+ c.getForevenId());
}
}
session.close();
System.out.println("One To Many is Done for selecting.....!");
factory.close();
}
}
|
What you are thinkig....
One Response to “Hibernate One to Many Select Query Example”
If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum




Plz some one tell me How i use one to many mapping in hibernate for creation of three tables.
1.discussion(discId,queId,ansId)
2.question(queId,question,postdate)
3.answer(ansId,answer,postDate)
Thanks in advance
Plz rply its argent