首页 > 代码库 > Spring - JPA 一对一, 一对多, 多对多关联

Spring - JPA 一对一, 一对多, 多对多关联

现在有三个类:One Many Much

One类
Much类

@Entity
public class Much {

	@Id
	@GeneratedValue
	private Integer id;
	
	private String name;
	
	@ManyToMany
	@JoinTable(
			name = "much_more",
			joinColumns= {@JoinColumn(name = "many_id", referencedColumnName = "id")},
			inverseJoinColumns= {@JoinColumn(name = "much_id", referencedColumnName = "id")})
	private List<Many> manys;

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public List<Many> getManys() {
		return manys;
	}

	public void setManys(List<Many> manys) {
		this.manys = manys;
	}
	
	
}

  

@Entity public class One { @Id @GeneratedValue private Integer id; private String name; // one to many @OneToMany(cascade = CascadeType.ALL, mappedBy = "one") // @JoinColumn(name = "many_id") private List<Many> manys; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

  

Many类

@Entity
public class Many {

	@Id
	@GeneratedValue
	private Integer id;
	
	private String name;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "one_id")
	private One one;
	
	public Integer getId() {
		return id;
	}

	
	@ManyToMany(mappedBy="manys")
	private List<Much> muchs;
	
	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public List<Much> getMuchs() {
		return muchs;
	}

	public void setMuchs(List<Much> muchs) {
		this.muchs = muchs;
	}

	public One getOne() {
		return one;
	}

	public void setOne(One one) {
		this.one = one;
	}
}

  

一对一:现有一个single类和One是一对一关系,则只需要在SIngle类中添加如下即可

@OneToOne(fetch = FETCHTYPE.EAGE)
private One one;

  此时生成的表结构是: 在Single表中有一个one_id字段指向one表的主键

 

一对多: 一个one对应多个many,

    首先在一端添加:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "one") //mappedBy = "one" 表示one是一对多管理的被维护端, 既当添加many时顺带添加一个one
private List<Many> manys;

  

  然后再多端添加:

@ManyToOne(fetch = FetchType.EAGER) 
@JoinColumn(name = "one_id") // 在多端(从表的外键)添加外键字段指向一端(主表的主键)的主键字段 private One one;

  

此时生成的表结构是:在Many表里有一个one_id外键指向一端One

 

 

多对多:

  拿many和much来说, 他们多对多关系, 我们先确定谁是多放关系的维护端,这里我指定Much为多方关系的维护端。

  多对多关系中,hibernate会自动生成中间表。

  在Much中添加:

Much类

	@ManyToMany
	@JoinTable(  // JoinTable所在的一端为多方关系的维护端
			name = "much_more", // 指定中间表名
			joinColumns= {@JoinColumn(name = "many_id", referencedColumnName = "id")}, // 指定当前表在中间表的外键名称和外键所指向的当前表主键
			inverseJoinColumns= {@JoinColumn(name = "much_id", referencedColumnName = "id")} // 指定另一方在中间表的外键名称和外键所指向的主键
           )
	private List<Many> manys;

  然后在多方关系的被维护端Many中添加:

@ManyToMany(mappedBy="manys") // mappedBy指定many为多方关系的被维护端
private List<Much> muchs;

  此时生成的表结构是:生成一个中间表叫much_more, 外键字段叫much_id,more_id分别指向两个表的主键

欢迎大家指正。

Spring - JPA 一对一, 一对多, 多对多关联