我试图以增量方式持久保存实体。
以下是Entity类的概述
package aop.web.teacher.rmodels;
// Generated 11 Feb, 2011 3:57:41 PM by Hibernate Tools 3.2.2.GA
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.validator.constraints.NotEmpty;
/**
* AopTeacherMaster generated by hbm2java
*/
@Entity
@Table(name="aop_teacher_master"
,schema="public"
)
public class AopTeacherMaster implements java.io.Serializable {
private long id;
private AopTeachersDistrictMaster aopTeachersDistrictMasterByCurrDistrict;
private AopInstitutionmaster aopInstitutionmaster;
private AopTeachersDistrictMaster aopTeachersDistrictMasterByPermDistrict;
@NotEmpty(message="Fathers name is mandatory")
private String fathersName;
@NotEmpty
private String currAddLine1;
private String currAddLine21;
private String currAddLine22;
private String currAddLine3;
private String currDevelopmentBlock;
private String currPoliceStation;
private String currCity;
private String currPin;
private String currState;
private String currCountry;
private String permAddLine1;
private String permAddLine21;
private String permAddLine22;
private String permAddLine3;
private String permDevelopmentBlock;
private String permPoliceStation;
private String permCity;
private String permPin;
private String permState;
private String permCountry;
private Date dateOfBirth;
private Character gender;
private Character maritalStatus;
private String religion;
private String caste;
private String landLineNumber;
private String mobileNumber;
private String email;
private String uidNum;
private String bankName;
private String bankBranch;
private String bankAccountNum;
private String gpfNum;
private Set aopTeachersSanctionedPostDetailsForCurrentIncumbentId = new HashSet(0);
private Set aopTeachersSanctionedPostDetailsForFirstIncumbentId = new HashSet(0);
public AopTeacherMaster() {
}
public AopTeacherMaster(long id) {
this.id = id;
}
现在我在第一个屏幕中有一个2步向导,就像进程一样 用户输入实体的一些属性,实体合并, 在第二步中,将填充额外的或其余的属性。
我正在使用基于spring 3注释的控制器,我正在使用实体类 作为命令对象。
在第一个去的实体用屏幕保存一个值,然后我发送 从合并引用作为第二个屏幕的命令对象。
但是,第二个屏幕似乎填充了那里的条目但是使其无效 来自第一个屏幕的现有属性。
以下是相同
的控制器代码
@RequestMapping(value = "/insertteacher.html", method = RequestMethod.POST)
public
String testEm(@Valid AopTeacherMaster teacher, BindingResult result,
Map model) {
logger.info("Checking Teacher for error");
if (result.hasErrors()) {
logger.info("User data has:" + result.getErrorCount() + " errors!");
// ////////////////////
for (Object object : result.getAllErrors()) {
if (object instanceof FieldError) {
FieldError fieldError = (FieldError) object;
logger.error("Error on field::" + fieldError.getField()
+ " || error type ::" + fieldError.getCode());
}
}
model.put("smessage", "There was an error");
return "teachersmasterInsert";
}
logger.info("Attemped saving!");
teacher=schoolMasterService.add(teacher);//recieved the reference after merge! Will be used for command object in the next screen
model.put("teacher", teacher);//This is the command object for second screen
model.put("smessage", "teacher inserted successfully");
return "teachersmasterInsert2";
// List myList=testDaoService.findAllTeachers();
// for(Teachermaster t:myList){logger.info("Got::"+t.getId());}
}
@RequestMapping(value = "/insertteacher2.html", method = RequestMethod.POST)
public
String testEm2(@Valid AopTeacherMaster teacher, BindingResult result,
Map model) {
logger.info("Checking Teacher for error second insert");
if (result.hasErrors()) {
logger.info("User data has:" + result.getErrorCount() + " errors!");
// ////////////////////
for (Object object : result.getAllErrors()) {
if (object instanceof FieldError) {
FieldError fieldError = (FieldError) object;
logger.error("Error on field::" + fieldError.getField()
+ " || error type ::" + fieldError.getCode());
}
}
model.put("smessage", "There was an error");
return "teachersmasterInsert";
}
logger.info("Attemped saving!");
teacher=schoolMasterService.add(teacher);
model.put("teacher", teacher);
model.put("smessage", "teacher second instance inserted successfully");
return "teachersmasterInsert";
// List myList=testDaoService.findAllTeachers();
// for(Teachermaster t:myList){logger.info("Got::"+t.getId());}
}
这是正确的方法吗?除此以外 我怎样才能实现这种增量保存? 请指教! 提前谢谢。
答案 0 :(得分:1)
在基于注释的控制器中实现向导的典型方法是在会话中存储部分构造的对象,并仅在最后一步之后保存它:
@Controller
// Model attribute with name "aopTeacherMaster" is transparently stored in the session
@SessionAttribute("aopTeacherMaster")
public class TeacherController {
...
@RequestMapping(value = "/insertteacher2.html", method = RequestMethod.POST)
public String testEm2(@Valid AopTeacherMaster teacher, BindingResult result, Map model) {
...
// No need to save teacher here
}
@RequestMapping(value = "/insertteacherLast.html", method = RequestMethod.POST)
public String testEmLast(@Valid AopTeacherMaster teacher, BindingResult result,
Map model, SessionStatus status) {
...
// Save teacher at the last step
teacher=schoolMasterService.add(teacher);
// Remove it from the session
status.setComplete();
}
}
或者,如果由于某种原因确实需要增量保存,则可以从数据库加载实体的当前状态,并手动复制包含模型对象数据的字段。