One-to-Many Hibernate Mapping Example

In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related to many (children). It is a 1 to N relationship.

For example, one vendor can have many customers; therefore, we need to take a collection property such as Set, Map, and List in the parent class.

Now, we are going to create an example, in which two persistent classes are available, i.e., Vendor (parent) and Customer (child). Here, we need to define @OneToMany and @JoinColumn annotations in the persistent class.

Following are the steps to create the example:

  1. Create all persistent classes

In this step, we are going to develop persistent classes, i.e., and

  • Create the configuration file

In this step, we are going to create the configuration class (hibernate.cfg.xml) which contains the information of persistent class and the database.


  • Create the main class that stores the object of the persistent class

In this step, we are going to create a class which consists of main() method, and stores the objects of persistent classes.

  • Output
One-to-Many Hibernate Mapping
  • Tables


Vendor One-to-Many Hibernate Mapping


Customers One-to-Many Hibernate Mapping


vendor_customers One-to-Many Hibernate Mapping

Pin It on Pinterest

Share This