Hibernate的单向N-N关联(四)

N-N关联需要使用@ManyToMany注解来修饰代表关联实体的集合。同时,N-N关联必须使用连接表。下面是单向N-N关联的Person类。

@Entity
@Table(name="person_inf")
public class Person
{
	// 标识属性
	@Id @Column(name="person_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	private String name;
	private int age;
	// 定义该Person实体所有关联的Address实体
	@ManyToMany(targetEntity=Address.class)
	// 映射连接表为person_address
	@JoinTable(name="person_address",
		// 定义连接表中名为person_id的外键列,该外键列参照当前实体对应表的主键列
		joinColumns=@JoinColumn(name="person_id"
		// 定义连接表中名为address_id的外键列,
		// 该外键列参照当前实体的关联实体对应表的主键列,没有指定unique=true
			, referencedColumnName="person_id"),
		inverseJoinColumns=@JoinColumn(name="address_id"
			, referencedColumnName="address_id")
	)
	private Set<Address> addresses
		= new HashSet<>();

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// name的setter和getter方法
	public void setName(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return this.name;
	}

	// age的setter和getter方法
	public void setAge(int age)
	{
		this.age = age;
	}
	public int getAge()
	{
		return this.age;
	}

	// addresses的setter和getter方法
	public void setAddresses(Set<Address> addresses)
	{
		this.addresses = addresses;
	}
	public Set<Address> getAddresses()
	{
		return this.addresses;
	}
}

下而Address的实体类。

@Entity
@Table(name="address_inf")
public class Address
{
	// 标识属性
	@Id @Column(name="address_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int addressId;
	// 定义地址详细信息的成员变量
	private String addressDetail;

	// 无参数的构造器
	public Address()
	{
	}
	// 初始化全部成员变量的构造器
	public Address(String addressDetail)
	{
		this.addressDetail = addressDetail;
	}

	// addressId的setter和getter方法
	public void setAddressId(int addressId)
	{
		this.addressId = addressId;
	}
	public int getAddressId()
	{
		return this.addressId;
	}

	// addressDetail的setter和getter方法
	public void setAddressDetail(String addressDetail)
	{
		this.addressDetail = addressDetail;
	}
	public String getAddressDetail()
	{
		return this.addressDetail;
	}
}

用下面的类来保存Person与Address.

public class PersonManager
{
	public static void main(String[] args)
	{
		PersonManager mgr = new PersonManager();
		mgr.testPerson();
		HibernateUtil.sessionFactory.close();
	}

	private void testPerson()
	{
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		// 创建一个Person对象
		Person p = new Person();
		// 设置Person的name为owen
		p.setName("owen");
		p.setAge(20);
		// 持久化Person对象
		session.save(p);
		// 创建一个瞬态的Address对象
		Address a = new Address("广州天河");
		// 设置Person和Address之间的关联关系
		p.getAddresses().add(a);
		// 再持久化Address对象
		session.persist(a);
		// 创建一个瞬态的Address对象
		Address a2 = new Address("上海虹口");
		// 设置Person和Address之间的关联关系
		p.getAddresses().add(a2);
		// 再持久化Address对象
		session.persist(a2);

		Person p2 = new Person();
		p2.setName("fkit");
		p2.setAge(29);
		p2.getAddresses().add(a2);
		session.save(p2);

		tx.commit();
		HibernateUtil.closeSession();
	}
}

SQL语句如下。

Hibernate: 
    insert 
    into
        person_inf
        (age, name) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        address_inf
        (addressDetail) 
    values
        (?)
Hibernate: 
    insert 
    into
        address_inf
        (addressDetail) 
    values
        (?)
Hibernate: 
    insert 
    into
        person_inf
        (age, name) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        person_address
        (person_id, address_id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        person_address
        (person_id, address_id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        person_address
        (person_id, address_id) 
    values
        (?, ?)

执行的结果图如下 :

Hibernate的单向N-N关联(四)_第1张图片




你可能感兴趣的:(java,Hibernate,jsp,ssh,Web应用)