[英]How to update data with Hibernate and Spring JPA in Spring MVC
First I'm just a beginner who like to learn Spring framework. 首先,我只是一个初学者,喜欢学习Spring框架。 I'm creating Simple online domestic flight reservation system using
Spring MVC
with Hibernate
and Spring JPA
. 我正在使用带有
Hibernate
和Spring JPA
Spring MVC
创建简单的在线国内航班预订系统。 This problem is raised when I'm going to update the name, email and contact no of registered users. 当我要更新注册用户的姓名,电子邮件和联系方式时,会出现此问题。
my-profile.jsp (Link for update-user.jsp) my-profile.jsp(update-user.jsp的链接)
<a href='<spring:url value="/account/my-profile/${user.id}.html"/>'>
<button type="button">My Profile</button>
</a>
update-user.jsp update-user.jsp
<form:form modelAttribute="userUpdate" method="POST" cssClass="userValidation" >
<div>
<label for="name">Name</label>
<div>
<form:input path="name" name="name"/>
<form:errors path="name"/>
</div>
</div>
<div>
<label for="email">Email</label>
<div class="col-sm-10">
<form:input path="email" name="email"/>
<form:errors path="email"/>
</div>
</div>
<div>
<label for="contactNum">Contact No</label>
<div>
<form:input path="contactNum" name="contactNum"/>
<form:errors path="contactNum"/>
</div>
</div>
</form:form>
UserController.java UserController.java
@RequestMapping(value="/account/my-profile/{id}", method={RequestMethod.POST})
public String showProfile(@PathVariable int id, ModelMap model,
@Valid @ModelAttribute("userUpdate") User user,
@ModelAttribute("user") User user2,
BindingResult result,Authentication authentication){
if(result.hasErrors()){
return "my-profile";
}
String name = authentication.getName();
User user3 = userService.findOne(name);
userService.save(user3);
return "redirect://flightInfos/booknow/payments.html?success=true";
}
UserService.java UserService.java
public void save(User user) {
user.setEnabled(true);
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
user.setPassword(encoder.encode(user.getPassword()));
List<Role> roles = new ArrayList<Role>();
roles.add(roleRepository.findByName("ROLE_USER"));
user.setRoles(roles);
userRepository.save(user);
}
public User findOne(String name) {
return userRepository.findByName(name);
}
UserRepository.java UserRepository.java
public interface UserRepository extends JpaRepository<User, Integer> {
User findByName(String name);
}
When I run this, no error is there and following sql
statement is shown in console. 当我运行此命令时,没有错误存在,并且控制台中显示以下
sql
语句。
Hibernate: select user0_.id as id1_4_, user0_.contactNum as contactN2_4_, user0_.email as email3_4_, user0_.enabled as enabled4_4_, user0_.name as name5_4_, user0_.password as password6_4_, user0_.userId as userId7_4_ from myUser user0_ where user0_.name is null
Hibernate: select user0_.id as id1_4_, user0_.contactNum as contactN2_4_, user0_.email as email3_4_, user0_.enabled as enabled4_4_, user0_.name as name5_4_, user0_.password as password6_4_, user0_.userId as userId7_4_ from myUser user0_ where user0_.email is null
Hibernate: select user0_.id as id1_4_, user0_.contactNum as contactN2_4_, user0_.email as email3_4_, user0_.enabled as enabled4_4_, user0_.name as name5_4_, user0_.password as password6_4_, user0_.userId as userId7_4_ from myUser user0_ where user0_.name=?
Hibernate: select user0_.id as id1_4_, user0_.contactNum as contactN2_4_, user0_.email as email3_4_, user0_.enabled as enabled4_4_, user0_.name as name5_4_, user0_.password as password6_4_, user0_.userId as userId7_4_ from myUser user0_ where user0_.email=?
Hibernate: select user0_.id as id1_4_, user0_.contactNum as contactN2_4_, user0_.email as email3_4_, user0_.enabled as enabled4_4_, user0_.name as name5_4_, user0_.password as password6_4_, user0_.userId as userId7_4_ from myUser user0_ where user0_.name=?
Hibernate: select role0_.id as id1_3_, role0_.name as name2_3_ from Role role0_ where role0_.name=?
Hibernate: update myUser set contactNum=?, email=?, enabled=?, name=?, password=?, userId=? where id=?
Hibernate: delete from myUser_Role where users_id=?
Hibernate: insert into myUser_Role (users_id, roles_id) values (?, ?)
When I run this program, no change is happened in the database. 当我运行该程序时,数据库中没有任何更改。 And also I can't log using new credentials or using previous credentials.
而且我也无法使用新凭据或以前的凭据进行日志记录。
I appreciate your help to find out this case. 感谢您为查明此案提供的帮助。 I need to update existing user.
我需要更新现有用户。 His name, email and contact no.
他的姓名,电子邮件和联系方式。 What I can do to solve this matter ?
我该怎么办才能解决这个问题?
Role
entity Role
实体
@Entity
public class Role {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany(mappedBy="roles")
private List<User> users;
getters and setters
User
entity User
实体
@Entity
@Table(name = "myUser")
public class User {
@Id
@GeneratedValue
private Integer id;
private String userId;
@Size(min=4, message="Name must be at least 4 characters.")
@UniqueUsername(message="Username exists")
@Column(unique=true)
private String name;
@Size(min=5, message="Password must be at least 5 characters.")
private String password;
@Size(min=1, message="Invalid email")
@Email(message="Invalid email")
@UniqueEmail(message="Email exists")
private String email;
private String contactNum;
private Boolean enabled;
@ManyToMany
@JoinTable
private List<Role> roles;
getters & setters
Where are you doing update? 您在哪里更新? In your controller you are just getting user from db and saving it right after.
在您的控制器中,您只是从db中获取用户,然后立即保存它。
User user3 = userService.findOne(name); userService.save(user3);
You shoud do update user3 with new data. 您应该使用新数据更新user3。 I would do it like this.
我会这样做。
User user3 = userService.findOne(name); updateUser(user3, user); userService.save(user3);
updateUser method: updateUser方法:
private void updateUser(oldUser, newUser){ oldUser.setName(newUser.getName()); oldUser.setEmail(newUser.getEmail()); oldUser.setContactNum(newUser.getContactNum()) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.