单向N-N关联 (必须使用连接表)
Address实体类
package com.anlw.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="address_inf")
public class Address {
//标识属性
@Id
@Column(name="address_id11d")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer addressId;
//定义地址详细的成员变量
private String addressDetail;
//无参构造器
public Address(){}
//初始化全部成员变量的构造器
public Address(String addressDetail) {
this.addressDetail = addressDetail;
}
public Integer getAddressId() {
return addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public String getAddressDetail() {
return addressDetail;
}
public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}
}
Person实体类
package com.anlw.entity;
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="person_inf")
public class Person{
//标识属性
@Id
@Column(name="person_id12")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private int age;
//定义该Person实体所有关联的Address实体
@ManyToMany(targetEntity=Address.class)
//映射连接表为p_a
@JoinTable(name="p_a",
//定义连接表中名为person_id的外键列,该外键列参照当前实体对应表的主键列
joinColumns=@JoinColumn(name="p",referencedColumnName="person_id12"),
//定义连接表中名为address_id的外键列
//该外键列参照当前实体的关联实体对应表的主键列,没有指定unique=true
inverseJoinColumns=@JoinColumn(name="a",referencedColumnName="address_id11d")
)
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
}
核心测试
package com.anlw.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Address;
import com.anlw.entity.Person;
public class Test {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();
ServiceRegistry st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(st);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
// 创建一个Person对象
Person p = new Person();
// 创建一个瞬态的Address对象
Address a = new Address("guangzhou");
// 必须先持久化Address对象
sess.persist(a);
p.setName("kobe");
p.setAge(36);
// 设置Person和Address之间的关联关系
p.getAddresses().add(a);
// 持久化Person对象
sess.save(p);
// 创建一个瞬态的Address对象
Address a2 = new Address("shanghai");
// 必须先持久化Address对象
sess.persist(a2);
// 修改持久化状态的Person对象
p.getAddresses().add(a2);
tx.commit();
sess.close();
sf.close();
}
}
单向的N-N关联和1-N关联的持久化类代码完全相同,控制着关系的一端需要增加一个Set类型的属性,被关联的持久化实例以集合的形式存在;
N-N关联需要使用@ManyToMany注解来修饰代表关联实体的集合属性;
@ManyToMany支持下面属性:
cascade:(非必须属性)指定Hibernate对关联实体采用怎样的级联策略,该级联策略支持如下五个属性:
1.CascadeType.ALL:指定Hibernate将多有的持久化操作都级联到关联实体;
2.CascadeType.MERGE:指定Hibernate将merge操作级联到关联实体;
3.CascadeType.PERSIST:指定Hibernate将persist操作级联到关联实体;
4.CascadeType.REFRESH:指定Hibernate将refresh操作级联到关联实体;
5.CascadeType.REMOVE:指定Hibernate将remove操作级联到关联实体;
fetch:(非必须属性)指定抓取关联实体时的抓取策略,该属性支持如下两个属性值:
1.FetchType.EAGER:抓取实体时,立即抓取关联实体,这是默认值;
2.FetchType.LAZY:抓取实体时,延迟抓取关联实体,等到正真用到关联实体时采取抓取;
targetEntity:(非必须属性)该属性指定关联实体的类名,在默认情况下,Hibernate将通过反射来判断关联实体的类名;
mappedBy:(非必须属性)该属性合法的属性值为关联实体的属性名,该属性指定关联实体中哪个属性可引用到当前实体;
N-N关联必须使用连接表,N-N关联和有连接表的1-N关联非常相似,因此都需要使用@JoinTable来映射连接表,区别是N-N关联需要去掉@JoinTable注解的inverseJoinColumns属性所指定的@JoinColumn中的unique=true;从上面的Person代码来看,指定inverseJoinColumns属性时,@JoinColumn注解没有指定unique=true属性,这就完成了N-N的关联映射;