Hibernate One to Many Example using Annotation
Tags: hibernate, jpa, sql, annotations
Published: 2022-10-26 04:52:09

Create the Maven project add mysql , hibernate , lombok dependencies as belows in pom.xml

Directory Structure

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eadh2332u7ohjmlyfhm3.png)

**pom.xml**
```
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.test</groupId>
	<artifactId>TimeStamp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>TimeStamp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.9</maven.compiler.source>
		<maven.compiler.target>1.9</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.6.8.Final</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.20</version>
			<scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.22</version>
		</dependency>

	</dependencies>
</project>
```

**Address.java**

```
package com.test.entity;

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "addrtab")
public class Address {
	@Id
	@Column(name="aid")
	private int addrId;
	private String loc;
	private String pin;
	
}

```
**Employee
**
```
package com.test.entity;

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "emptab")
public class Employee {
	
	@Id
	@Column(name = "eid")
	private int empId;
	
	@Column(name = "ename")
	private String empName;
	
	@Column(name = "esal")
	private double empSal;
	
	@ManyToOne
	@JoinColumn(name = "adiFk")//fk column
	private Address addr;
}
```

**HibernateUtil.java**
```

package com.test.util;

import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

import com.test.entity.Address;
import com.test.entity.Employee;

public class HibernateUtil {
	
	static SessionFactory factory = null;
	static {
		Configuration cfg = new Configuration();
		
		Properties props = new Properties();
		props.put(Environment.URL, "jdbc:mysql://localhost:3306/new");
		props.put(Environment.USER, "root");
		props.put(Environment.PASS, "root");
		props.put(Environment.SHOW_SQL, true);
		props.put(Environment.FORMAT_SQL, true);
		props.put(Environment.HBM2DDL_AUTO, "update");
		
		cfg.setProperties(props);
		cfg.addAnnotatedClass(Employee.class);
		cfg.addAnnotatedClass(Address.class);
		
		factory = cfg.buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory() {
		return factory;
	}
	public static Session getSession() {
		return factory.openSession();
	}
}

```

**Test.java**
```
package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.test.entity.Address;
import com.test.entity.Employee;
import com.test.util.HibernateUtil;

public class Test {
	public static void main(String[] args) {
		Transaction tx = null;
		//get the SessionFactory 
		SessionFactory factory = HibernateUtil.getSessionFactory();
		//get the Session
		Session ses = HibernateUtil.getSession();
		try(factory;ses){
			//begin the tx
			tx = ses.beginTransaction();
			Address addr = new Address();
			addr.setAddrId(101);
			addr.setLoc("pune");
			addr.setPin("431605");
			
			//emp objects
			Employee e1 = new Employee();
			e1.setEmpId(1);
			e1.setEmpName("Sam");
			e1.setEmpSal(4000.0);
			e1.setAddr(addr);
			
			Employee e2 = new Employee();
			e2.setEmpId(2);
			e2.setEmpName("Jhon");
			e2.setEmpSal(5000.0);
			e2.setAddr(addr);
			
			Employee e3 = new Employee();
			e3.setEmpId(3);
			e3.setEmpName("Brock");
			e3.setEmpSal(6000.0);
			e3.setAddr(addr);
			
			ses.save(addr);			
			ses.save(e1);			
			ses.save(e2);			
			ses.save(e3);
			
			
			//commit the tx
			tx.commit();
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
```