我正在尝试使用 userID 更新和合并同一行数据库中的新数据。 我的代码运行顺畅,没有错误,但在设置值后,我的数据库不会更新。 我应该如何获取我的更新值到我的数据库表中?我卡住了..请帮帮我
这是我试图运行的代码段。首先我匹配 NIC 号码以从表中获取 userID ,然后为该ID设置新值
/ XHrUserDao类代码 /
public Integer findPersonXHrUserId(String nic)
{
Query query = em.createQuery("select u.userId from XHrUser u where (u.nic) = (:nic)")
.setParameter("nic", nic);
List<Integer> list = query.getResultList();
return list.isEmpty() || list.get(0) == null ? 0 : list.get(0);
}
public void UpdateUserInDbTable(String firstName, String lastName, String nic, String phoneNumber, String emailAddress) {
int userId = findPersonXHrUserId(nic);
XHrUser xHrUserObj= new XHrUser();
xHrUserObj.setUserId(userId);
xHrUserObj.setFirstName(firstName);
xHrUserObj.setLastName(lastName);
xHrUserObj.setNic(nic);
xHrUserObj.setPhone(phoneNumber);
xHrUserObj.setEmail(emailAddress);
xHrUserObj= em.find(XHrUser.class, xHrUserObj.getUserId());
em.merge(xHrUserObj);
em.flush();
em.refresh(xHrUserObj);
}
/ XHrUser类代码 /
package hr;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity //tells that its an entity class
@Table(name = "x_hr_user") //specify the name of the db table to be used
public class XHrUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id //indicating its the primary key of the table
@Basic(optional = false)
@Column(name = "user_id")
private Integer userId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "nic")
private String nic;
@Column(name = "phone")
private String phone;
@Column(name = "email")
private String email;
public XHrUser() {
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getNic() {
return nic;
}
public void setNic(String nic) {
this.nic = nic;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
/ UserService Class /
package hr;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
@Stateless
public class UserService implements Serializable {
private static final long serialVersionUID = 1L;
@EJB
private XHrUserDao userDao;
public void UpdateUser(String firstName,String lastName,String nic,String phoneNumber, String emailAddress)
{
userDao.UpdateUserInDbTable(firstName, lastName, nic,phoneNumber,emailAddress);
}
}
/ 支持班级 / / 它从用户获取数据并将其传递给服务类 /
package hr;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class GetUpdatedUserBacking implements Serializable {
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private String nic;
private String phoneNumber;
private String emailAddress;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getNic() {
return nic;
}
public void setNic(String nic) {
this.nic = nic;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@EJB
private UserService userService;
public GetUpdatedUserBacking () {
}
public String UpdatingInfor()
{
String response;
UpdateUserInformation(firstName, lastName, nic,phoneNumber,emailAddress);
response="ViewUpdatedDetails.xhtml";
return(response);
}
public void UpdateUserInformation(String firstName,String lastName,String nic,String phoneNumber, String emailAddress){
userService.UpdateUser(firstName, lastName, nic,phoneNumber,emailAddress);
}
}
答案 0 :(得分:0)
似乎autocommit
设置为false
,然后您可以执行以下操作
EntityTransaction txn = em.getTransaction();
txn.begin();
em.merge(xHrUserObj);
em.flush();
txn.commit();
答案 1 :(得分:0)
em.find()从数据库获取状态,并替换您使用setter创建和更新的User对象。首先尝试从数据库中获取实体,然后通过setter更新它(无需更改id)并合并它。
int userId = findPersonXHrUserId(nic);
XHrUser xHrUserObj= em.find(XHrUser.class, userId);
xHrUserObj.setFirstName(firstName);
xHrUserObj.setLastName(lastName);
xHrUserObj.setNic(nic);
xHrUserObj.setPhone(phoneNumber);
xHrUserObj.setEmail(emailAddress);
em.merge(xHrUserObj);
em.flush();
em.refresh(xHrUserObj);