码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate单向关联N-1

时间:2015-08-04 21:01:44      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:hibernate   关联   n-1   

无连接表的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两个表关联起来,还有这两个表都没有增加外键列。

数据表:
技术分享
技术分享
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Hibernate单向关联N-1

标签:hibernate   关联   n-1   

原文地址:http://blog.csdn.net/csujiangyu/article/details/47281843

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!