Java Spring Boot Hibernate不更新现有对象

时间:2018-12-18 14:18:38

标签: java spring hibernate

Im编码以及一些udemy教程,但是我尝试使用thymeleaf而不是jsp。这是我的表单html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="eng">

<head>
  <meta charset="UTF-8" />
  <title>Save Customer</title>
  <link rel="stylesheet" type="text/css" th:href="@{/style.css}" />
  <link rel="stylesheet" type="text/css" th:href="@{/add-customer-style.css}" />
</head>

<body>
  <div class="wrapper">
    <div class="header">
      <h2>CRM - Customer Relationship Manager</h2>
    </div>
  </div>
  <div class="container">
    <h3>Save Customer</h3>
    <form action="#" th:action="@{/customer/saveCustomer}" th:object="${customer}" method="post">

      <!--need to associate this data with customer id-->
      <input type="hidden" th:field="*{id}">

      <table>
        <tbody>
          <tr>
            <td><label for="">First name:</label></td>
            <td><input type="text" th:field="*{firstName}"></td>
          </tr>

          <tr>
            <td><label for="">Last name:</label></td>
            <td><input type="text" th:field="*{lastName}"></td>
          </tr>

          <tr>
            <td><label for="">email:</label></td>
            <td><input type="text" th:field="*{email}"></td>
          </tr>

          <tr>
            <td><label for=""></label></td>
            <td><input type="submit" value="save" class="save"></td>
          </tr>
        </tbody>
      </table>
    </form>

    <p>
      <a th:href="@{/customer/list}">Back to list</a>
    </p>
  </div>
</body>

</html>

这是我的道指:

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import javax.persistence.EntityManagerFactory;
import java.util.List;
 
@Repository
public class CustomerDAOImpl implements CustomerDAO {
 
    //need to inject the session factory
    private SessionFactory hibernateFactory;
 
    @Autowired
    public CustomerDAOImpl(EntityManagerFactory factory) {
        if(factory.unwrap(SessionFactory.class) == null){
            throw new NullPointerException("factory is not a hibernate factory");
        }
        this.hibernateFactory = factory.unwrap(SessionFactory.class);
    }
 
    @Override
    public List<Customer> getCustomers() {
 
        //get the current hibernate session
        var currentSession = hibernateFactory.openSession();
 
        //create a query... sort by last name
        var theQuery = currentSession.createQuery("from Customer order by lastName", Customer.class);
 
        // execute query and get result list
 
        //return the results
        return theQuery.getResultList();
    }
 
    @Override
    public void saveCustomer(Customer theCustomer) {
//        get current hibernate session
        var currentSession = hibernateFactory.openSession();
 
//        save the customer... finally LOL
        currentSession.saveOrUpdate(theCustomer);
    }
 
    @Override
    public Customer getCustomer(int theId) {
//        get the current hibernate session
        var currentSession = hibernateFactory.openSession();
 
//        now retrieve/read from database using the primary key
        return currentSession.get(Customer.class, theId);
    }

当我单击更新时,它将用我要更新的对象数据填充表单。但是单击“保存”后,没有任何更改。知道为什么吗?我浪费第二天试图使它工作...

PS 会话工厂可能有问题吗?

还是隐藏输入?

1 个答案:

答案 0 :(得分:0)

尝试在调用@Transactional的服务方法上添加saveCustomer