| Newsletter |
Hibernate One To Many Annotation Example
Hibernate » on Aug 30, 2011 { 15 Comments } By Sivateja
Let us see an example on one to many annotations mapping…
Files required..
- Customers.java
- Vendor.java
- ForOurLogic.java
- hibernate.cfg.xml
Customers.java
package str;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Customers")
public class Customers{
@Id
@Column(name = "custid")
private int customerId;
@Column(name = "custName", length=10)
private String customerName;
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.java
package str;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Vendor")
public class Vendor{
@Id
@Column(name = "vid")
private int vendorId;
@Column(name = "vname", length=10)
private String vendorName;
@OneToMany(fetch=FetchType.LAZY, targetEntity=Customers.class, cascade=CascadeType.ALL)
@JoinColumn(name = "venid", referencedColumnName="vid")
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;
}
}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></mapping> <mapping></mapping> </session-factory> </hibernate-configuration>
ForOurLogic.java
package str;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class ForOurLogic {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Vendor v=new Vendor();
v.setVendorId(100);
v.setVendorName("java4s");
Customers c1=new Customers();
c1.setCustomerId(500);
c1.setCustomerName("customer1");
Customers c2=new Customers();
c2.setCustomerId(501);
c2.setCustomerName("customer2");
Set s=new HashSet();
s.add(c1);
s.add(c2);
v.setChildren(s);
Transaction tx=session.beginTransaction();
session.save(v);
tx.commit();
session.close();
System.out.println("One to Many Annotatios Done...!!!!!!");
factory.close();
}
}Output
![]() | ![]() |
![]() | ![]() |
You Might Also Like
::. About the Author .:: | ||
![]() | ||
Comments
15 Responses to “Hibernate One To Many Annotation Example”



Hi Admin,
Your code is executing perfectly,But i think is not right way.B’coz if the operation faild to insert values into chaild table then the insertion of parent table also should be rollback.But with this code i can’t get such outcome.(If i am wrong please correct me.)
Thaks & Regords
raju.
Configuration cfg = new Configuration();
cfg.configure(“hibernate.cfg.xml”);
this is not required.
Pls correct it..
Yes, it's not required. But, if the config file is with a different name, then you need to explicitly mention it's name. Nothing serious.
Configuration cfg = new Configuration();
cfg.configure(“hibernate.cfg.xml”);
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
so we have to write Configuration and AnnotationConfiguration??
Configuration cfg = new Configuration();
cfg.configure(“hibernate.cfg.xml”);
The above code not required.
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
this code works good in hibernate version 4. But will not work till hibernate version 3. For that we
need AnnotationConfiguration class i.e.
Configuration cfg=new AnnotationConfiguration();
cfg.configure("hibernate.cfg.xml");
this code is required. AnnotationConfiguration this class is deprecated from version 4. So for compatibility for both the version he has wrote both the codes.
hii…..!
u r just providing example….and not providing any explanation regarding @OneToMany annnotation and its attributes….!
perfect…add for retrieval also..!!!!!!!
Exception in thread "main" org.hibernate.MappingException: <mapping> element in configuration specifies no attributes
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1326)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1285)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1267)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1234)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1162)
at com.str.ForOurLogic.main(ForOurLogic.java:18)
Where will i get require jar file to run the code
Great help
Not clear.. Can somebody give more details about one to many relationship!!!
Not working getting
Exception in thread "main" org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="str.Vendor"/>
Can please provide what are the jar files required for this application.
What is use of @Transient in hibernate in entity class