欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

oneToMany,ManyToOne双向关联

程序员文章站 2022-03-02 15:30:13
...

package com.xuguang.ext.xgerp.comm.workshop.bean;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.FetchType;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

/**
* 工作点
* @author ztw
*
*/
@Entity
@Table(name="comm_workshop")
public class WorkShop implements Serializable {

/**
*
*/
private static final long serialVersionUID = -7502127838260795796L;
/**
* 关键字
*/
private String workshopPK;
/**
* 编号
*/
private String workshopNo;
/**
* 名称
*/
private String workshopName;
/**
* 类型
*/
private String workshopType;
/**
* 父
*/
private WorkShop father;
/**
* 子
*/
private List<WorkShop> sons;
@Id
public String getWorkshopPK() {
return workshopPK;
}
public void setWorkshopPK(String workshopPK) {
this.workshopPK = workshopPK;
}
public String getWorkshopNo() {
return workshopNo;
}
public void setWorkshopNo(String workshopNo) {
this.workshopNo = workshopNo;
}
public String getWorkshopName() {
return workshopName;
}
public void setWorkshopName(String workshopName) {
this.workshopName = workshopName;
}
public String getWorkshopType() {
return workshopType;
}
public void setWorkshopType(String workshopType) {
this.workshopType = workshopType;
}

@ManyToOne(targetEntity=WorkShop.class)
@JoinColumn(name="fatherPK")
@NotFound(action=NotFoundAction.IGNORE)
public WorkShop getFather() {
return father;
}
public void setFather(WorkShop father) {
this.father = father;
}

@OneToMany(mappedBy="father",fetch=FetchType.EAGER)
@OrderBy("workshopName")
public List<WorkShop> getSons() {
return sons;
}
public void setSons(List<WorkShop> sons) {
this.sons = sons;
}
}