Hibernate 注解单向一对多配置

来源:互联网 发布:两张excel找出相同数据 编辑:程序博客网 时间:2024/04/29 17:08
***
 * 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());
}
}
 
}


}


0 0
原创粉丝点击