Spring Data Jpa一对多单向映射

/**

  • @author StormMaybin
  • @date 2017-01-17
    */

生命不息,奋斗不止!


一对多映射关系

在JPA中,用@OneToMany来标识一对多的关系。实现一对多的单向关联,只需在代表一的实体(Company)中使用@OneToMany映射标注就可以了,代表多的实体不需要使用任何映射标注。

有两种方式实现一对多的单向关联。一种是在只使用@OneToMany来标识,这种方式是通过一张第三方表来保存关系。还有一种是使用@OneToMany和@JoinColumn来标注,这种方式是在多的一方(Employee)的表中增加一个外键列来保存关系。

OneToMany

Company.java

package com.stormma.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Created by mayongbin01 on 2017/1/17.
 */
@Entity
@Table(name = "company")
public class Company {

    //唯一标识公司
    @Id
    @GeneratedValue
    private int id;

    //公司名字
    private String name;

    @OneToMany(cascade = {CascadeType.ALL})
    private List<Employee> employees;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Employee.java

package com.stormma.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created by mayongbin01 on 2017/1/17.
 */
@Entity
@Table(name = "employee")
public class Employee {

    //唯一标识雇员
    @Id
    @GeneratedValue
    private int id;

    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

生成的表结构

CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `company_employees`;
CREATE TABLE `company_employees` (
  `company_id` int(11) NOT NULL,
  `employees_id` int(11) NOT NULL,
  UNIQUE KEY `UK_lg2r1rg13q18sa62l1y7un4or` (`employees_id`),
  KEY `FKd66w6jx84tydyd8sf9mpqh5je` (`company_id`),
  CONSTRAINT `FKd66w6jx84tydyd8sf9mpqh5je` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`),
  CONSTRAINT `FKnntnqhhla66c4h9ddbnlvqk2x` FOREIGN KEY (`employees_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


OneToMany+JoinColumn

Company.java

package com.stormma.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Created by mayongbin01 on 2017/1/17.
 */
@Entity
@Table(name = "company")
public class Company {

    //唯一标识公司
    @Id
    @GeneratedValue
    private int id;

    //公司名字
    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "company_id")
    private List<Employee> employees;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Employee.java

package com.stormma.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created by mayongbin01 on 2017/1/17.
 */
@Entity
@Table(name = "employee")
public class Employee {

    //唯一标识雇员
    @Id
    @GeneratedValue
    private int id;

    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

结构

Hibernate: create table company (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table employee (id integer not null auto_increment, name varchar(255), company_id integer, primary key (id))
Hibernate: alter table employee add constraint FK5v50ed2bjh60n1gc7ifuxmgf4 foreign key (company_id) references company (id)

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    会飞的鱼69阅读 21,153评论 18 385
  • 个人的JPA参考手册,尚未整理完毕,会更新 githu地址:https://github.com/jcalaz/t...
    jcala阅读 3,633评论 2 48
  • 本文中我们介绍并比较两种最流行的开源持久框架:iBATIS和Hibernate,我们还会讨论到Java Persi...
    大同若鱼阅读 2,986评论 4 26
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 71,155评论 12 116
  • 萌萌哒的小动物o(≧v≦)o~~ 发现两个问题:一是线条的流畅度,二是头和身子的比例。我的小老鼠比例失调了(⊙o⊙...
    一只小橘猫阅读 37评论 2 1