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

  • hibernate.cfg.xml

package str;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "Customers")
public class Customers{

	@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;


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;

@Table(name = "Vendor")

public class Vendor{

	@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;



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

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></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="">update</property>


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();

		SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = factory.openSession();		

	      Vendor v=new Vendor();

	      Customers c1=new Customers();

	      Customers c2=new Customers();

	      Set s=new HashSet();


	      Transaction tx=session.beginTransaction();

		System.out.println("One to Many Annotatios Done...!!!!!!");





You Might Also Like

  ::. About the Author .::

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


15 Responses to “Hibernate One To Many Annotation Example”
  1. raju says:

    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

  2. Configuration cfg = new Configuration();

    this is not required.
    Pls correct it..

    • rakesh says:

      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.

  3. sasi says:

    Configuration cfg = new Configuration();

    SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();

    so we have to write Configuration and AnnotationConfiguration??

  4. Mohan says:

    Configuration cfg = new Configuration();

    The above code not required.

    • shantanu Surve says:

      Configuration cfg=new Configuration();
      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();

      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.

  5. Ashoka says:


    u r just providing example….and not providing any explanation regarding @OneToMany annnotation and its attributes….!

  6. radheshyam says:

    perfect…add for retrieval also..!!!!!!!

  7. aman says:

    Exception in thread "main" org.hibernate.MappingException: <mapping> element in configuration specifies no attributes
    at org.hibernate.cfg.Configuration.parseMappingElement(
    at org.hibernate.cfg.Configuration.parseSessionFactory(
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.configure(
    at com.str.ForOurLogic.main(

  8. sundar says:

    Where will i get require jar file to run the code

  9. arun singh says:

    Great help

  10. udaya says:

    Not clear.. Can somebody give more details about one to many relationship!!!

  11. Arpit says:

    Not working getting
    Exception in thread "main" org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="str.Vendor"/>

  12. nag says:

    Can please provide what are the jar files required for this application.

  13. Dileep Balinenii says:

    What is use of @Transient in hibernate in entity class


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 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.