我有用户(员工)的数据库
@Entity
@Table(name = "user")
public class User {
public enum Role{
USER,ADMIN
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "company", nullable = false)
private String company;
private String title;
@Enumerated(EnumType.STRING)
@Column(name = "role", nullable = false)
private Role role = Role.USER;
public User() {
}
public User(String firstName, String lastName, String email, String password, String company, String title)
{
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.company = company;
this.title = title;
}
我有UserDao.java
@Repository
public interface UserDao extends JpaRepository<User,Long> {
User findByEmail(String email);
}
当然还有UserController.java
@RequestMapping(value = "/edit-active-user/{email:.+}", method = RequestMethod.GET)
public String editActiveUserAgainGet(@PathVariable String email, Model model) {
System.out.println(email.toString());
User user = userDao.findByEmail(email);
model.addAttribute("user", user);
return "editUser";
}
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String saveUserAgain(@ModelAttribute User user) {
userDao.save(user);
return "redirect:/";
}
在名为editUser的jsp文件中,我的表单包含例如:
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-6">
<input required value="${user.title}" name="title" type="text" id="title" class="form-control" placeholder="Enter your job title" autofocus>
</div>
</div>
根据每个用户的属性无密码。
在更新属性期间,所有属性都会再次保存(甚至其中一些还没有更改)但密码为NULL,因为它不是JSP格式。
答案 0 :(得分:1)
如果您想要更新,我们只说一个字段,那么您可以在存储库中编写另一个带有@Query
和@Modifying
注释的方法,例如:
@Modifying
@Query("update user u set u.email = ?1 where u.id = ?2")
int setEmail(String email, Long id);
答案 1 :(得分:0)
也许你需要传递hibernate dynamic-update = true。有关详细信息,请阅读以下https://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/
<强>更新强>
DECLARE @sampledata AS TABLE
(
CustomerID int,
ProductID int
)
INSERT INTO @sampledata VALUES (111, 6577),(111,6123 ),(111,1133 ),(111 ,1133)
SELECT *,
CONCAT(s.CustomerID,'-',CAST(dense_rank() over(PARTITION BY s.CustomerID ORDER BY s.ProductID DESC) AS varchar(10))) AS ref
FROM @sampledata s
如果您传递updatable false,它将永远不会在更新中使用,仅在插入中使用。