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

数据表:

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

时间: 2024-10-25 16:09:37

Hibernate单向关联N-1的相关文章

Hibernate单向关联N-N

单向N-N关联必须使用连接表. Company实体: package com.ydoing.hibernate5; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue;

Hibernate单向关联1-N

基于外键1-N关联(无连接表) 一个Customer关联多个Card Customer实体(1端): package com.ydoing.hibernate4; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.per

Hibernate单向关联1-1

基于外键的单向1-1关联 一个House关联一个Owner. House类: package com.ydoing.hibernate3; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Generation

一口一口吃掉Hibernate(五)——一对多单向关联映射

版权声明:本文为博主原创文章,未经博主允许不得转载.如需转载请声明:[转自 http://blog.csdn.net/xiaoxian8023 ] 在上一篇博客<一口一口吃掉Hibernate(四)--多对一单向关联映射>中,介绍了多对一的关联映射,今天就反过来说一下一对多的单向关联映射. 可能有人会对这2篇博客的题目有点混淆不清,跟日常说的关系有点不同.我们日常说的比如父子关系,夫妻关系都是说的双向关系,而现在讨论的则是单向关系,所以也就有了多对一和一对多的说法. 二者的关系其实很简单,只是

Hibernate 一对多单向关联Demo

以Classes[班级]和Student[学生]为例的Demo Classes .java public class Classes implements Serializable { private long Cid; private String Cname; private String Cdesc; private Set<Student> students; //get和set } Student .java public class Student implements Seria

【Hibernate步步为营】--单向关联一对一映射(一)

上篇文章对多对一的关联映射做了详细的分析,它在实现上可以有两种方式,并且这两种方式实现也很简单,关键是标签<many-to-one>的使用,它分别指明了多端和一端的映射关系,这种映射关系既是对象模型中的聚合关系.接下来继续讨论关联映射. 一.唯一外键 唯一外键说的是数据库表中的每一行的外键唯一对应着另一张表中的主键,也就是说一个表的主键作为另一张表的外键,并且它们之间的关系是唯一的,这种反应到关系模型中如下图所示: 上图的两个实体表,分别为人和身份证,很明显的一个人对应着一个身份证.身份证作为

Hibernate之关于多对多单向关联映射

[Hibernate]之关于多对多单向关联映射 老师和学生,最典型的多对多关联, Teacher和Student.所谓单向意思就是说.老师知道自己的教的是哪些学生而学生不知道是哪些老师教. 也能够这么说,在查询的时候,通过老师能够级联查询出学生,可是通过学生不能够级联查询出老师. 而多对多最麻烦的是怎么自己定义我们中间表的,表名和列名,这个是重要的! Annotations配置 @Entity @Table(name="t_teacher") publicclass Teacher {

hibernate之多对一单向关联

一个工作组(Group)里可以有多个用户(User),一个User只属于一个Group,这是典型的多对一的关系.在多对一的关系中正确的数据库设计是在多的这方(在这里是User这方)加一个Group的外键.如果数据库设计的与之相反就会产生冗余,请看下面这个例子: 友情提示:这是错误的设计方法: GroupId GroupName UserId 1 Group_1 1 1 Group_1 2 UserId UserName 1 moluo 2 xingzhe 这样在一的这方(也就是Group这方)设

Hibernate ORM框架——续第二章:Hibernate映射关系:单向关联

一:课堂笔记 **********单表映射*************** 1.只有一个实体类映射一个表 2.有一个实体类+ 一个或者多个值类型,合在一起 映射为一个表 **********多表映射*************** 定义:类有多个映射为多个表 **********数据库层面,如ORM无关************** 2个表之间要建立关系?该如何做?有多少种方法 假定有2个表,分别为A,B 3种关系 a)主键关联:A表的主键对应B表的主键,(一对一的关系) 在数据库种如何建立这种关系?