您的位置:首页 > 其它

Hibernate 注解单向一对多配置

2016-08-30 07:54 309 查看
***

 * Plate表

 * @author Swing

 *

 */
@Entity

@Table(name = "tb_Plate")

public class Plate implements Serializable  {
private static final long serialVersionUID = 1L;
private int id;
private String PlateName;
private Date addTime;
private String addName;
private String Jurisdiction;
private String typeName;
 

private List<SubPlate> subPlate;

@OneToMany(fetch = FetchType.EAGER,targetEntity = SubPlate.class,cascade =      //单项一对多配置
{
CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.MERGE,
})
@JoinColumns(value={@JoinColumn(name="Pid",referencedColumnName="id")})   //对应关系 Pid = id
public List<SubPlate> getSubPlate() {
return subPlate;
}
public void setSubPlate(List<SubPlate> subPlate) {
this.subPlate = subPlate;
}
@Id
@GeneratedValue

 
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlateName() {
return PlateName;
}
public void setPlateName(String plateName) {
PlateName = plateName;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
this.addName = addName;
}
public String getJurisdiction() {
return Jurisdiction;
}
public void setJurisdiction(String jurisdiction) {
Jurisdiction = jurisdiction;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}

}

/***

 * SubPlate表

 * @author Swing

 *

 */
@Entity

@Table(name = "tb_SubPlate")

public class SubPlate {
private static final long serialVersionUID = 1L;
private int id;
private String SubPlateName;
private Date addTime;
private String addName;
private int Pid;
private String Jurisdiction;

 
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubPlateName() {
return SubPlateName;
}
public void setSubPlateName(String subPlateName) {
SubPlateName = subPlateName;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
this.addName = addName;
}

public int getPid() {
return Pid;
}
public void setPid(int pid) {
Pid = pid;
}
public String getJurisdiction() {
return Jurisdiction;
}
public void setJurisdiction(String jurisdiction) {
Jurisdiction = jurisdiction;
}

}

测试方法 HQL语句:

public class text {
public static void main(String[] args) {
Service service = new ServiceImpl();
List list =  service.listQuery("select p from Plate p left join fetch p.subPlate e");
for(Plate p:(List<Plate>) list){
System.out.println("Plate:"+p.getPlateName());
 
for(SubPlate e:p.getSubPlate()){
System.out.println("Sub"+e.getSubPlateName());
}
}
 
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: