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 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 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)

你可能感兴趣的:(Spring Data Jpa一对多单向映射)