Spring Data Jpa many unidirectional mapping

/**

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

life is long so add oil!


To-many mapping relationship

In JPA, with @OneToMany to identify many relationship. One-way-many association, only a representative entity (Company) used @OneToMany map marked on it, representing the number of entities do not need to use any mapping annotations.

There are two ways to achieve many of unidirectional association. One is to use only @OneToMany is identified in this way is to save the relationship by a third-party table. Another use is to mark @JoinColumn @OneToMany and, in this way is to add a foreign key column in the multi-party (the Employee) to hold the relationship table.

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;
    }
}

Generating a configuration table

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;
    }
}

structure

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)
Original Address: https: //www.jianshu.com/p/0a2163273b3e

Guess you like

Origin www.cnblogs.com/jpfss/p/11058093.html