无连接表的N-1关联
多个Employee关联一个Department。
Employee实体(N端):
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;
// N-1单向关联
@ManyToOne(targetEntity = Department.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
// @JoinColumn(name = "department_id", nullable = false)
@JoinTable(name = "employee_department", // 连接表名
joinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "employee_id", unique = true), inverseJoinColumns = @JoinColumn(name = "department_id", referencedColumnName = "department_id"))
private Department department;
....省略get和set方法...
Department实体(1端):
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;
....省略get和set方法...
测试:
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输出:
Hibernate:
insert
into
department_inf
(name)
values
(?)
Hibernate:
insert
into
employee_inf
(age, department_id, name)
values
(?, ?, ?)
从以上输出不能看出,Hibernate先插入主表address_inf,然后再持久化从表employee_inf。并没有连接表生成。但是employee_inf多出来外键列department_id。
数据表:
有连接表的N-1关联
还是用以上的列子,修改Employee类:
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单向有连接表关联
@ManyToOne(targetEntity = Department.class, cascade = CascadeType.ALL)
// @JoinColumn(name = "department_id", nullable = false)
@JoinTable(name = "employee_department", // 连接表名
joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "person_id", unique = true), inverseJoinColumns = @JoinColumn(name = "department_id", referencedColumnName = "department_id"))
private Department department;
....省略get和set方法...
Console输出:
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
(?, ?)
从输出可以看出,Hiberate主动创建了employee_department表,并把employee_inf和department_inf两个表关联起来,还有这两个表都没有增加外键列。
数据表:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-25 16:09:37