Hibernate tutorial 3 : Hibernate "Many to One" association
Hibernate tutorial 4 : “Many To One” association
A sample code to learn how to map “Many To One” relationship between two entities using the Hibernate ORM.
Create only database, don’t create tables (tables will be created by Hibernate)
CREATE DATABASE `persist_db` /*!40100 DEFAULT CHARACTER SET utf8 */
<dependencies>
<!-- MySQL connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.11.Final</version>
</dependency>
</dependencies>
<?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>
<!-- MySQL -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/persist_db?useTimezone=true&serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- Hibernate -->
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Entities -->
<mapping class="net.isetjb.hibernatetutorial4.Product"></mapping>
<mapping class="net.isetjb.hibernatetutorial4.Category"></mapping>
</session-factory>
</hibernate-configuration>
Source entity : Product.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "product")
public class Product
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private int id;
@Column(name = "name", length = 255, nullable = true)
private String name;
@Column(name = "price", nullable = true)
private int price;
@ManyToOne
@JoinColumn(name = "category_id", foreignKey = @ForeignKey(name = "CATEGORY_ID_FK"))
private Category category;
// Getters and Setters here...
}
Destination entity : Category.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "category")
public class Category
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private int id;
@Column(name = "name", length = 255, nullable = true)
private String name;
// Getters and Setters here...
}
example :
// new category
Category category_a = new Category();
category_a.setName("Cat a");
session.save(category_a);
// new product
Product product_x = new Product();
product_x.setName("Prod x");
product_x.setPrice(456);
product_x.setCategory(category_a);
session.save(product_x);