省和市的 对应管理 为一对多:
1.对应pojo (Province.java和City.java):
(1)Province.java
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/** 省
*
* @pdOid 009f5c9b-914c-44ff-b82a-94c41b9d1abb */
@Entity(name="Province")
@Table(name="province")
public class Province implements java.io.Serializable {
/** 主键
*
* @pdOid 4cdf9132-ed58-4ddc-a02e-7bb33aceb88e */
public java.lang.Integer id;
/** 省名称
*
* @pdOid 1d107605-655a-4eb6-b7d9-edd50a9e6908 */
public java.lang.String name;
/** 编号
*
* @pdOid 5131c56e-fb66-452a-ab43-aa5321038f40 */
public java.lang.Integer code;
private Set<City> citys = new HashSet<City>();
@OneToMany(targetEntity =City.class,mappedBy="fatherID", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public Set<City> getCitys() {
return citys;
}
public void setCitys(Set<City> citys) {
this.citys = citys;
}
public Province() {
// TODO Add your own initialization code here.
}
/**
* Get value of id
*
* @return id
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", nullable=false, insertable=true, updatable=true, length=11)
public java.lang.Integer getId()
{
return id;
}
/**
* Set value of id
*
* @param newId
*/
public void setId(java.lang.Integer newId)
{
this.id = newId;
}
/**
* Get value of name
*
* @return name
*/
@Basic(optional=true)
@Column(name="province", insertable=true, updatable=true, length=20)
public java.lang.String getName()
{
return name;
}
/**
* Set value of name
*
* @param newName
*/
public void setName(java.lang.String newName)
{
this.name = newName;
}
/**
* Get value of code
*
* @return code
*/
@Basic(optional=true)
@Column(name="provinceID", insertable=true, updatable=true, length=11)
public java.lang.Integer getCode()
{
return code;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((code == null) ? 0 : code.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Province other = (Province) obj;
if (code == null) {
if (other.code != null)
return false;
} else if (!code.equals(other.code))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
/**
* Set value of code
*
* @param newCode
*/
public void setCode(java.lang.Integer newCode)
{
this.code = newCode;
}
}
(2)City.java
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/** 省
*
* @pdOid 009f5c9b-914c-44ff-b82a-94c41b9d1abb */
@Entity(name="City")
@Table(name="city")
public class City implements java.io.Serializable {
/** 主键
*
* @pdOid 4cdf9132-ed58-4ddc-a02e-7bb33aceb88e */
public java.lang.Integer id;
/** 省名称
*
* @pdOid 1d107605-655a-4eb6-b7d9-edd50a9e6908 */
public java.lang.String name;
/** 编号
*
* @pdOid 5131c56e-fb66-452a-ab43-aa5321038f40 */
public java.lang.Integer code;
public Province fatherID;
private Set<Area> areas = new HashSet<Area>();
@OneToMany(targetEntity =Area.class,mappedBy="fatherID", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public Set<Area> getAreas() {
return areas;
}
public void setAreas(Set<Area> areas) {
this.areas = areas;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((code == null) ? 0 : code.hashCode());
result = prime * result + ((fatherID == null) ? 0 : fatherID.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
City other = (City) obj;
if (code == null) {
if (other.code != null)
return false;
} else if (!code.equals(other.code))
return false;
if (fatherID == null) {
if (other.fatherID != null)
return false;
} else if (!fatherID.equals(other.fatherID))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@ManyToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumn(name="fatherID", referencedColumnName="provinceID")//外键为sut_id,与student中的id关联
public Province getFatherID() {
return fatherID;
}
public void setFatherID(Province fatherID) {
this.fatherID = fatherID;
}
public City() {
// TODO Add your own initialization code here.
}
/**
* Get value of id
*
* @return id
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", nullable=false, insertable=true, updatable=true, length=11)
public java.lang.Integer getId()
{
return id;
}
/**
* Set value of id
*
* @param newId
*/
public void setId(java.lang.Integer newId)
{
this.id = newId;
}
/**
* Get value of name
*
* @return name
*/
@Basic(optional=true)
@Column(name="city", insertable=true, updatable=true, length=20)
public java.lang.String getName()
{
return name;
}
/**
* Set value of name
*
* @param newName
*/
public void setName(java.lang.String newName)
{
this.name = newName;
}
/**
* Get value of code
*
* @return code
*/
@Basic(optional=true)
@Column(name="cityID", insertable=true, updatable=true, length=11)
public java.lang.Integer getCode()
{
return code;
}
/**
* Set value of code
*
* @param newCode
*/
public void setCode(java.lang.Integer newCode)
{
this.code = newCode;
}
}
注意:
(1).@OneToMany(targetEntity =City.class,mappedBy="fatherID", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 这里mappedBy对应值填写City类中定义的Province属性fatherID( public Province fatherID;)
(2). @JoinColumn(name="fatherID", referencedColumnName="provinceID")name填写city表中的外键字段(实际表中可以没有外键约束),referencedColumnName填写,city表外键关联的province表中具体字段,而不是Province类中的字段
下面是标的结构
(1)province表
(2)city表