gpt4 book ai didi

java - spring JPA增量保存实体

转载 作者:行者123 更新时间:2023-12-01 15:53:21 27 4
gpt4 key购买 nike

我正在尝试增量地持久保存一个实体。

这是实体类的概述


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;
}

现在我有一个类似向导的两步流程,位于第一个屏幕中用户输入实体的一些属性并且实体被合并,在第二步中,填充附加或其余属性。

我正在使用基于 spring 3 注解的 Controller ,其中我使用实体类作为命令对象。

第一次实体被保存为屏幕一个值,然后我发送来自合并的引用作为第二个屏幕的命令对象。

但是,第二个屏幕似乎填充了那里的条目,但使来自第一个屏幕的现有属性。

这是相同的 Controller 代码


@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());}

}

这是正确的方法吗?否则我怎样才能实现这种增量保存?请建议!提前致谢。

最佳答案

在基于注释的 Controller 中实现向导的典型方法是将部分构造的对象存储在 session 中,并仅在最后一步之后保存它:

@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();
}
}

或者,如果由于某种原因确实需要增量保存,您可以从数据库加载实体的当前状态,并手动从模型对象中复制包含数据的字段。

关于java - spring JPA增量保存实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5562205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com