码迷,mamicode.com
首页 > Web开发 > 详细

注解:【有连接表的】Hibernate双向1->N关联

时间:2016-05-06 10:48:11      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

Person与Address关联:双向1->N,【有连接表的】,N端控制关联关系

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

// 双向1->N:1对N关联关系
// 定义该Person实体所有关联的Address
@OneToMany(targetEntity = Address.class, //
mappedBy = "person") // 指定mappedBy属性,表明该person实体不控制关联关系。
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;
}

}

Address.java

package org.crazyit.app.domain;

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.ManyToOne;
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实体
@ManyToOne(targetEntity = Person.class)
@JoinTable(name = "person_address", // 指定连接表名称
// 指定连接表中address_id列参照当前实体对应数据表的address_id主键列
joinColumns = @JoinColumn(name = "address_id", //
referencedColumnName = "address_id", //
unique = true) , // 增加唯一性约束(unique),保证address只对应一个person实体
// 直接连接表中person_id列参照当前实体关联实体对应数据列表的person_id主键列
inverseJoinColumns = @JoinColumn(name = "person_id", //
referencedColumnName = "person_id") //
)
private Person 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 Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}

}

#数据模型

技术分享

注解:【有连接表的】Hibernate双向1->N关联

标签:

原文地址:http://www.cnblogs.com/xiaowei1763369680/p/5464634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!