/**

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

    */


一对多映射关系

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

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

    }

    }

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

    }

    }

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)

原文地址:https://www.jianshu.com/p/0a2163273b3e

05-11 13:49