在我的Seam3.2.2集成的JSF项目中,Update不起作用。而是更新该值,它将新值插入表中。
我需要一些帮助。
我的项目流程。
pages.xml中:
<page view-id="/updateUser.xhtml" action="#{userEditAction.setCurrentUser()}">
<navigation>
<rule if-outcome="success">
<redirect view-id="/secure/admin/user/updateUser.xhtml"/>
</rule>
</navigation>
<begin-conversation join="true"></begin-conversation>
</page>
UserEditAction.java Bean:
@RequestParameter
private Integer userId;
@Override
public String setCurrentUser(){
System.out.println("Selected userid ----- " + userId);
currentUser = userService.getUser(userId);
return "success";
}
userUpdate.xhtml:
<h:commandButton value="Update" tabindex="20"
action="#{userAction.update}">
<f:param name="userId" value="#{currentUser.userId}" />
</h:commandButton>
userAction.java Bean
@Name("userAction")
@Stateless
@AutoCreate
public class UserActionImpl implements UserAction {
@Out(value = "currentUser", scope = ScopeType.CONVERSATION)
@In(value = "currentUser", scope = ScopeType.CONVERSATION, required = false)
private CLRPUser currentUser;
@In
private UserService userService;
@RequestParameter
private Integer userId;
private static final long serialVersionUID = 8282995226262125676L;
public String update() {
userService.saveOrUpdate(currentUser);
return "success";
}
userService.java:
public Integer saveOrUpdate(CLRPUser user) {
// TODO Validation...check cell phone number.. email id..
// user.setLastUpdatedBy(currentUserId);
user.setLastUpdatedBy(loggedInUser.getId());
user.getResidentialAddress().setLastUpdatedBy(loggedInUser.getId());
user.getShippingAddress().setLastUpdatedBy(loggedInUser.getId());
userMapper.java:
public class UserMapper {
public static UserEntity mapUserEntity(CLRPUser user) {
UserEntity userEntity = new UserEntity();
userEntity.setEmailAddress(user.getEmailAddress());
userEntity.setFirstName(user.getFirstName());
userEntity.setLastName(user.getLastName());
userEntity.setHomeNumber(user.getHomeNumber());
userEntity.setOfficeNumber(user.getOfficeNumber());
userEntity.setCellNumber(user.getCellNumber());
AuditInfo auditInfo = new AuditInfo();
auditInfo.setLastUpdatedBy(user.getLastUpdatedBy());
auditInfo.setLastUpdatedTime(user.getLastUpdatedTime());
userEntity.setAuditInfo(auditInfo);
userEntity.setType(user.getType());
userEntity.setPreferenceId(user.getPreferenceId());
userEntity.setASPCustomerId(user.getAspCustomerId());
user.getResidentialAddress().setAddressType("R");
user.getShippingAddress().setAddressType("S");
AddressEntity residential = AddressMapper.mapAddressEntity(user
.getResidentialAddress());
AddressEntity shipping = AddressMapper.mapAddressEntity(user
.getShippingAddress());
List<AddressEntity> addressess = new ArrayList<AddressEntity>();
addressess.add(residential);
addressess.add(shipping);
userEntity.setAddresses(new HashSet<AddressEntity>(addressess));
/*UserLoginEntity userLogin = UserLoginMapper.mapUserLoginEntity(user.getUserLogin());
List<UserLoginEntity> userLoginn = new ArrayList<UserLoginEntity>();
userLoginn.add(userLogin);
userEntity.setUserLogin(new HashSet<UserLoginEntity>(userLoginn));*/
// userEntity.setCardTypeEntity(cardTypeEntity)
return userEntity;
}
JPAUserDAO.java
public Integer saveOrUpdate(UserEntity newUser) {
entityManager.merge(newUser);
return newUser.getUserId();
}
答案 0 :(得分:0)
<强>问题: - 强>
插入正在发生,因为每次创建UserEntity
实体的新实例时都会更新,而不是设置其ID或主键。
所以在保存hibernate时认为实体是新的,每次都会生成新的id导致插入
<强>解决方案: - 强>
要更正mapUserEntity()
方法
userEntity.setId(user.getId());
所以mapUserEntity()
看起来像
public static UserEntity mapUserEntity(CLRPUser user) {
UserEntity userEntity = new UserEntity();
userEntity.setId(user.getId());//Note:-added this line
userEntity.setEmailAddress(user.getEmailAddress());
userEntity.setFirstName(user.getFirstName());
userEntity.setLastName(user.getLastName());
...
}