2017-03-23 69 views
0

你好,我想更新用戶的實體,所以在形式edit.html我使用thymeleaf我的實體:我不能更新使用Hibernate,Spring MVC的,Thymleaf

<form action="#" th:action="@{/{id}/edit(id=${user.id})}" 
          th:object="${user}" method="post"> 
          <div class="md-form"> 
           <input type="text" th:field="*{firstName}" 
            th:value="${user.firstName}" name="firstName" id="firstName" 
            class="form-control" /> <label class="active" for="firstName">Prénom</label> 
          </div> 
          <div class="md-form"> 
           <input type="text" th:field="*{lastName}" 
            th:value="${user.lastName}" name="lastName" id="lastName" 
            class="form-control" /> <label class="active" for="lastName">Nom</label> 
          </div> 
          <div class="md-form"> 
           <input type="text" th:field="*{email}" th:value="${user.email}" 
            name="email" id="email" class="form-control" /> <label 
            class="active" for="email">email</label> 
          </div> 
          <div class="md-form"> 
           <input type="text" th:field="*{password}" 
            th:value="${user.password}" name="password" id="password" 
            class="form-control" /> <label class="active" for="password">mot 
            de passe</label> 
          </div> 
          <div class="md-form"> 
           <input type="text" th:field="*{occupation}" 
            th:value="${user.occupation}" name="occupation" id="occupation" 
            class="form-control" /> <label class="active" for="occupation">profession</label> 
          </div> 
          <div class="md-form"> 
           <input type="text" th:field="*{ville}" th:value="${user.ville}" 
            name="ville" id="ville" class="form-control" /> <label 
            class="active" for="ville">ville</label> 
          </div> 
          <div> 
           <input class="btn btn-sm btn-primary waves-effect btn-rounded" 
            type="submit" value="modifier" /> 
          </div> 
         </form> 

User.java:

@Entity 
@Table(name = "users") 
public class User { 
    @Id 
    @GeneratedValue 
    private Long id; 
    private String firstName; 
    private String lastName; 
    private String userName; 
    private int age; 
    private String ville; 
    private String email; 
    private String password; 
    private String phone; 
    private String company; 
    private String occupation; 
    private String img_profil; 

    @ManyToMany(mappedBy = "users", cascade = { CascadeType.ALL }) 
    private Set<Discussion> discussion; 

UserController.java

@GetMapping("/{userName}/edit") 
public String editUser(@PathVariable String userName, Model model) { 
    User user = ur.findByUserName(userName).get(0); 
    model.addAttribute(user); 
    return "edit"; 
} 

@PostMapping("/{id}/edit") 
public String updateUser(@ModelAttribute("user") User user, @PathVariable("id") Long id) { 
    user = ur.findOne(id); 
    ur.save(user); 
    return "redirect:/find/" + user.getUserName(); 
} 

UserRepository.java

@Repository 
public interface UserRepository extends CrudRepository<User, Long> { 
    List<User> findByUserName(String userName); 

} 

問題是在控制檯中我沒有看到更新請求(由Hibernate生成),沒有什麼是對數據庫的更改。

+4

你從數據庫中獲取一個用戶,然後保存你剛剛得到了用戶。所以沒有什麼可以保存的。 –

+0

我認爲你是對的,那麼如何更新我的實體用戶? – mosab

+0

和你一樣,但在保存之前修改它的一些屬性(即不忽略updateUser方法的用戶參數)。我也會使這個方法成爲事務性的,這將允許在單個事務中加載和更新用戶,並且不需要調用save()。 –

回答

0

請在UserController.java以下變化:

@PostMapping("/{id}/edit") 
public String updateUser(
    @ModelAttribute("user") User user, 
    @PathVariable("id") Long id) 
{ 
     //user = ur.findOne(id); 
     user.setId(id); 
     ur.save(user); 
     return "redirect:/find/" + user.getUserName(); 
} 
相關問題