Newsletter |
Hibernate One to Many Select Query Example
Hibernate » on Jul 22, 2011 { 10 Comments } By Sivateja
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(); } }
You Might Also Like
::. About the Author .:: | ||
Comments
10 Responses to “Hibernate One to Many Select Query Example”
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
hi… the information given by you is very impressive , and can you provide information about the “Detached Criteria”. Thanks
Hi,
what is “insert” attribute in mapping files. Can you explain clearly?
nicely explained but…pls tell me why we set “insert=false” in hbm.xml….
nice example! 🙂 & cleared my concept
Wonderful exaples Siva Tej really great job thank you soo much
is there any way to select all parent objects with some chidren? say all vendors that has customer name = John
thank u so much for this example
Hi Java4s,
Not able to see Hibernate Relationships In Depth link(https://www.java4s.com/hibernate/hibernate-relationships/). Please provide us the link. Thanks!
Hi Java4s,
Can you please provide explanation for lazy loading in hibernate xml?.
Thanks