我正在使用 Spring MVC 3 和 Hibernate 开发一个应用程序。我有 2 个具有父子关系的类 User 和 TenancyDetails 。在编辑详细信息功能中,我们可以编辑 User 类的详细信息并动态添加 classTenancyDetails 项并保存更改。我正在为此使用 hibernate saveorupdate() 方法,并且 hibernate 在应该更新现有记录时正在为类 User 创建一条新记录。这是我的代码。
模型类:
@Entity
@Table(name="tenancydetails")
public class TenancyDetails {
@Column(name="tenancyID")
@Id
@GeneratedValue
private int tenancyId;
@Column(name="landlordName")
@NotNull
private String landlordName;
@Column(name="landLordEmail")
@NotNull
private String landlordEmail;
@Column(name="fromDate")
@NotNull
private Date fromDate;
@Column(name="toDate")
@NotNull
private Date toDate;
@Column(name="location")
@NotNull
private String location;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="userId", nullable=false)
private User user;
@Transient
protected Object[] jdoDetachedState;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getTenancyId() {
return tenancyId;
}
public void setTenancyId(int tenancyId) {
this.tenancyId = tenancyId;
}
public String getLandlordName() {
return landlordName;
}
public void setLandlordName(String landlordName) {
this.landlordName = landlordName;
}
public String getLandlordEmail() {
return landlordEmail;
}
public void setLandlordEmail(String landlordEmail) {
this.landlordEmail = landlordEmail;
}
public Date getFromDate() {
return fromDate;
}
public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
}
public Date getToDate() {
return toDate;
}
public void setToDate(Date toDate) {
this.toDate = toDate;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
@Entity
@Table(name="User")
public class User {
@Column(name="userId")
@Id
private int userId;
@Column(name="name")
private String name;
@Column(name="emailId")
@NotEmpty(message = "Please enter your email id")
@Size(max = 50, message = "Email id can only be upto 50 characters long")
private String emailId;
@Column(name="password")
@NotEmpty(message = "Please enter your password")
@Size(max = 20, message = "Password can only be upto 20 characters long")
private String password;
@OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN,
org.hibernate.annotations.CascadeType.PERSIST,
org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name="userId"/*, nullable=true*/)
@LazyCollection(LazyCollectionOption.FALSE)
private List<TenancyDetails> tenancyDetails = null;
public User(){
tenancyDetails = new AutoPopulatingList<TenancyDetails>(TenancyDetails.class);
}
public List<TenancyDetails> getTenancyDetails() {
return tenancyDetails;
}
public void addTenancyDetail(TenancyDetails tenancyDetail) {
if (!tenancyDetails.contains(tenancyDetail)) {
tenancyDetails.add(tenancyDetail);
}
}
public void setTenancyDetails(List<TenancyDetails> tenancyDetails) {
this.tenancyDetails = tenancyDetails;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Controller :
@RequestMapping(value = "/Profile/{userId}", method = RequestMethod.POST)
public ModelAndView saveProfileDetails(@PathVariable int userId, Model model, HttpServletRequest request, HttpServletResponse response,
HttpSession session, ModelMap modelMap, @ModelAttribute("user") User user) {
System.out.println("am in save");
model.addAttribute("user", user);
System.out.println(user.getPassword());
System.out.println(user.getTenancyDetails().get(0).getUser().getUserId());
System.out.println(model.containsAttribute("user"));
tenantRatingService.saveProfileDetails(user);
return new ModelAndView("editProfile","user", user);
}
DAO方法:
public void saveProfileDetails(User user){
System.out.println("inside the save profile method");
Session session = getSession();
Transaction transaction = session.beginTransaction();
System.out.println("before save: " + user.getPassword());
System.out.println("before save: " + user.getTenancyDetails().get(0).getUser().getUserId());
session.saveOrUpdate(user);
transaction.commit();
session.flush();
System.out.println(user.getPassword());
}
我还将这两个类的 id 字段设置为 mysql 中的 AUTO_INCRMENT 字段。 我使用的一对多映射或 @GenerateValue 是否有问题?或者与 saveorupdate() 方法有关?在这种情况下我应该使用 merge() 吗?就在 saveorupdate() 语句之前,现有 userId 的值被正确打印出来!
有人可以告诉我我哪里出了问题吗?提前致谢。
最佳答案
我不知道你怎么写user
您的 Model
中的对象渲染表单时,最终会到达您的 POST 方法 "/Profile/{userId}"
。 id
您的user
对象成为0
。这就是为什么saveOrUpdate
方法保存您的 user
再次。要验证它,您可以在进入 saveProfileDetails
之前添加以下行方法:
user.setUserId(userId);
您的问题将会得到解决。
您还可以通过输入 @SessionAttributes("user")
来解决此问题,在您的 "/Profile/{userId}"
的所属 Controller 之前的GET
和POST
方法。
另请参阅:
关于java - Hibernate saveorupdate 或 merge 用于保存/更新一对多关系对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283339/