我有一个父表和子表和给他们的OneToOne关系...所以当我向子表添加一些记录父表的pimary键值(假设这个表已经有一些记录)我想添加在我的孩子表...我如何使用hibenate实现它...任何帮助..
这是我的父豆---
package com.valto.workflow.bean;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="TBL_WFProcessStatus_MST")
public class WorkFlowProcessStatus implements Serializable {
private static final long serialVersionUID = -5508699872054752924L;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="WFPStatusId")
private long WFPStatusId;
@Column(name="WFPStatusName")
private String WFPStatusName;
@Column(name="WFPStatusColor")
private String WFPStatusColor;
public long getWFPStatusId() {
return WFPStatusId;
}
public void setWFPStatusId(int wFPStatusId) {
WFPStatusId = wFPStatusId;
}
public String getWFPStatusName() {
return WFPStatusName;
}
public void setWFPStatusName(String wFPStatusName) {
WFPStatusName = wFPStatusName;
}
public String getWFPStatusColor() {
return WFPStatusColor;
}
public void setWFPStatusColor(String wFPStatusColor) {
WFPStatusColor = wFPStatusColor;
}
}
这是我的孩子班
package com.valto.workflow.bean;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="tbl_workflow_process_details")
public class WorkFlowProcessDetails implements Serializable{
private static final long serialVersionUID = -4355499783855489148L;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int WFProcessId;
@Column(name="WFProcessDate")
private String WFProcessDate;
@Column(name="ProcessComment")
private String ProcessComment;
@OneToOne
@JoinColumn(name="WFPStatusId")
private WorkFlowProcessStatus workflowprocessstatus;
@OneToOne
public int getWFProcessId() {
return WFProcessId;
}
public void setWFProcessId(int wFProcessId) {
WFProcessId = wFProcessId;
}
public String getWFProcessDate() {
return WFProcessDate;
}
public void setWFProcessDate(String wFProcessDate) {
WFProcessDate = wFProcessDate;
}
public String getProcessComment() {
return ProcessComment;
}
public void setProcessComment(String processComment) {
ProcessComment = processComment;
}
public WorkFlowProcessStatus getWorkflowprocessstatus() {
return workflowprocessstatus;
}
public void setWorkflowprocessstatus(WorkFlowProcessStatus workflowprocessstatus) {
this.workflowprocessstatus = workflowprocessstatus;
}
}
这是我在chld类中保存记录的代码....
public int saveWorkFlowProcessDetails(String wFProcessDate,
String processComment) {
Session sess=SessionFactoryCode.getSessionFactory().openSession();
try
{
WorkFlowProcessDetails wfpd=new WorkFlowProcessDetails();//child class object
wfpd.setWFProcessDate(wFProcessDate);
wfpd.setProcessComment(processComment);
sess.beginTransaction();
sess.save(wfpd);
WorkFlowProcessStatus wfps=new WorkFlowProcessStatus();//parent class object
wfpd.setWorkflowprocessstatus(wfps);
sess.update(wfpd);
sess.getTransaction().commit();
return 1;
}
catch(QueryException ex)
{
ex.getMessage();
sess.getTransaction().rollback();
sess.close();
return 0;
}
}
答案 0 :(得分:0)
您应该级联workflowprocessstatus
,以便在更新/保存WorkFlowProcessDetails
时将其保留。
@Entity
@Table(name="tbl_workflow_process_details")
public class WorkFlowProcessDetails implements Serializable{
private static final long serialVersionUID = -4355499783855489148L;
...
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="WFPStatusId")
private WorkFlowProcessStatus workflowprocessstatus;