【问题标题】:java spring boot hibernate doesnt update existing objectjava spring boot hibernate不更新现有对象
【发布时间】:2018-12-18 14:18:37
【问题描述】:

我正在编写一些 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>

这是我的 dao impl:

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 会话工厂可能有问题?

还是隐藏输入?

【问题讨论】:

  • 请将相关代码添加到问题中,而不是链接到粘贴代码。
  • 欢迎堆栈溢出。请不要发布指向代码、xml 等的链接,而是将它们包含在您的问题中。
  • 你绕过了 Spring 事务管理,为什么要使用 SessionFactory 而不是常规的 Entitymanager
  • 因为教程中使用了会话工厂。作为一个初学者,我不知道哪个更好。
  • 啊,伙计!它的工作!请重播我的主题,以便我可以标记解决方案:) 我使用 entityManager 而不是 session,一切都很完美!

标签: java spring hibernate


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    相关资源
    最近更新 更多