Hibernate unidirectional Association N-1
N-1 Association for connectable
Multiple employees are associated with one Department.
Employee entity (N-side ):
Package com. ydoing. hibernate2; import javax. persistence. cascadeType; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. fetchType; import javax. persistence. generatedValue; import javax. persistence. generationType; import javax. persistence. id; import javax. persistence. joinColumn; import javax. persistence. joinTable; import javax. persistence. manyToOne; import javax. persistence. table; @ Entity @ Table (name = employee_inf) public class Employee {@ Id @ Column (name = employee_id) @ GeneratedValue (strategy = GenerationType. IDENTITY) private Integer id; // One-Way Association of N-1 @ ManyToOne (targetEntity = Department. class, cascade = CascadeType. ALL, fetch = FetchType. LAZY) // @ JoinColumn (name = department_id, nullable = false) @ JoinTable (name = employee_department, // join table name joinColumns = @ JoinColumn (name = employee_id, referencedColumnName = employee_id, unique = true), inverseJoinColumns = @ JoinColumn (name = department_id, referencedColumnName = department_id) private Department ;.... omitting the get and set methods...
Department entity (1 side ):
Package com. ydoing. hibernate2; 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 = department_inf) public class Department {@ Id @ Column (name = department_id) @ GeneratedValue (strategy = GenerationType. IDENTITY) private Integer id; private String name ;.... omitting the get and set methods...
Test:
package com.ydoing.hibernate2;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.junit.BeforeClass;import org.junit.Test;public class Main { private static Session session; @BeforeClass public static void init() { Configuration conf = new Configuration(); conf.configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()) .build(); SessionFactory factory = conf.buildSessionFactory(serviceRegistry); session = factory.openSession(); } @Test public void test() { Employee emp = new Employee(); emp.setName(Jack); emp.setAge(20); emp.setDepartment(new Department(dev)); Transaction tx = session.getTransaction(); tx.begin(); session.save(emp); tx.commit(); session.close(); }}
Console output:
Hibernate: insert into department_inf (name) values (?)Hibernate: insert into employee_inf (age, department_id, name) values (?, ?, ?)
From the above output, it cannot be seen that Hibernate inserts the ss_inf of the main table first, and then persistently inserts the INF ss_inf from the table. No connected table is generated. However, the foreign key column "department_id" is exclusive to "employee_inf.
Data Table:
N-1 associations with connection tables
Use the preceding columns to modify the Employee class:
Package com. ydoing. hibernate2; import javax. persistence. cascadeType; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. generationType; import javax. persistence. id; import javax. persistence. joinColumn; import javax. persistence. joinTable; import javax. persistence. manyToOne; import javax. persistence. table; @ Entity @ Table (name = employee_inf) public class Employee {@ Id @ Column (name = person_id) @ GeneratedValue (strategy = GenerationType. IDENTITY) private Integer id; // N-1 one-way connected table Association @ ManyToOne (targetEntity = Department. class, cascade = CascadeType. ALL) // @ JoinColumn (name = department_id, nullable = false) @ JoinTable (name = employee_department, // join table name joinColumns = @ JoinColumn (name = person_id, referencedColumnName = person_id, unique = true), inverseJoinColumns = @ JoinColumn (name = department_id, referencedColumnName = department_id) private Department ;.... omitting the get and set methods...
Console output:
Hibernate: insert into department_inf (name) values (?)Hibernate: insert into employee_inf (age, name) values (?, ?)Hibernate: insert into employee_department (department_id, person_id) values (?, ?)
From the output, we can see that Hiberate has created the employee_department table and joined the two tables, employee_inf and department_inf, without adding foreign key columns.
Data Table: