Person与Address关联:双向N->N,【连接表必须有】,两端都控制关联关系
#需要说明的是:如果程序希望某一端放弃控制关联关系,则可以在这一段的@ManyToMany注解中指定mappedBy属性,这一端就
# 无需也不能使用@JoinTable映射连接表了。
Person.java
package org.crazyit.app.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Person_inf")
public class Person {
@Column(name = "person_id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
// 双向N->N:N对N关联关系
// 定义该Person实体所有关联的Address
@ManyToMany(targetEntity = Address.class)
@JoinTable(name = "person_address", //要求N端、N端的连接表名称相同
// 指定连接表中person_id列参照当前实体对应数据表的person_id主键列
joinColumns = @JoinColumn(name = "person_id", //
// 指定连接表中address_id列参照当前实体的关联实体对应数据表的address_id主键列
referencedColumnName = "person_id") , //
inverseJoinColumns = @JoinColumn(name = "address_id", //
referencedColumnName = "address_id") //
)
private Set<Address> addresses = new HashSet<Address>();
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 Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Adress.java
package org.crazyit.app.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Address_inf")
public class Address {
@Id
@Column(name = "address_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
// 定义该Address实体关联的person实体
@ManyToMany(targetEntity = Person.class)
@JoinTable(name = "person_address", // 指定连接表名称
// 指定连接表中address_id列参照当前实体对应数据表的address_id主键列
joinColumns = @JoinColumn(name = "address_id", //
referencedColumnName = "address_id") , //
// 直接连接表中person_id列参照当前实体关联实体对应数据列表的person_id主键列
inverseJoinColumns = @JoinColumn(name = "person_id", //
referencedColumnName = "person_id") //
)
private Set<Person> persons = new HashSet<Person>();
// 地址详细
private String addressDetail;
// 无参构造方法
public Address() {
}
// 初始化全部成员变量
public Address(String addressDetail) {
this.addressDetail = addressDetail;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddressDetail() {
return addressDetail;
}
public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
}
#数据模型