简体   繁体   中英

java spring boot hibernate doesnt update existing object

Im coding along with some udemy tutorial but i try to use thymeleaf instead of jsp. This is my form 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> 

and this is my 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); } 

when i click update, it fills form with object's data that i want to update. But after clicking save nothing changes. Any idea why? Im wasting second day trying to make it work...

PS Maybe something is wrong with session factory?

Or with hidden input?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM