spring JPA incrementally save an entity
I am trying to persist an entity incrementally.
Here is an overview of the Entity class
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;
}
Now I have a 2 step wizard like process where in the first screen user enters some of the properties of the entity and the entity gets merged, In the second step additional or rest of the properties are filled.
I am using spring 3 annotation based controller where I am using the entity class as the command object.
On the first go entity gets saved with screen one values then I am sending the reference from merge as command object for second screen.
However, second screen seems to populate the entries there but nullifies the existing properties which were from the first screen.
Here is the controller code for the same
@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());}
}
Is this the correct way to do it? Otherwise how can I achieve this incremental save? Please suggest! thanks in advance.
The typical way to implement wizards in annotation based controller is to store partially constructed object in the session, and save it only after the last step:
@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();
}
}
Alternatively, if you really need incremental save for some reason, you can load the current state of the entity from the database and copy fields with data from the model object manually.
精彩评论