关闭

Hibernate 注解单向一对多配置

255人阅读 评论(0) 收藏 举报
分类:
***
 * 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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:110904次
    • 积分:1951
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:170篇
    • 译文:3篇
    • 评论:11条
    文章分类
    最新评论