<!-- these are connection pool properties for C3P0 --> <propertyname="minPoolSize"value="5" /> <propertyname="maxPoolSize"value="20" /> <propertyname="maxIdleTime"value="30000" /> </bean>
@Override @Transactional public List<Customer> getCustomers(){ //get the current session Session session = sessionFactory.getCurrentSession(); // create a query Query<Customer> query = session.createQuery("from Customer", Customer.class); // Execute the query List<Customer> res = query.getResultList(); // return the result return res; } }
@Repository
Always apply @Repository before your DAOImplementation class.
It’s a subclass and inherits from the @Component, so it will be available for auto-scanning of component scanning.
Spring wil automatically register the DAO implementation.
Spring will also translate any JDBC related exceptions to unchecked exceptions.
Therefore, Repository is specific for code that talks to a data source or back-end repository.
@Transactional
@Transactional will automatically begin and end a transaction for your Hibernate code. So no need for you to explicitly do this.
Always apply @Transactional in your @Override methods.
3. Create Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@Controller @RequestMapping("/customer") publicclassCustomerController{ // need to inject Customer DAO into controller
@Autowired private CustomerDAO customerDAO;
@RequestMapping("/list") public String listCustomer(Model theModel){ // get customers from the DAO List<Customer> customers = customerDAO.getCustomers(); // add the customers to the Model theModel.addAttribute("customers", customers); return"list-customer"; } }
Note: Add <form:hidden path="id"/> into customer form. Very Important!
Track the selected Customer by storing the Id in the HTML form. Then the backend controller can retrive the customer by Id.
3. Process Form Data
1. Controller
1 2 3 4 5 6 7 8 9 10
@GetMapping("/showFormForUpdate") public String updateCustomer( @RequestParam("customerId")int theId, Model theModel){ // get the customer from service Customer customer = customerService.getCustomer(theId); // set the customer as a model attribute to pre-populate the form theModel.addAttribute("customer", customer); return"customer-form"; }
@Override publicvoidsaveCustomer(Customer customer){ Session session = sessionFactory.getCurrentSession(); session.saveOrUpdate(customer); // save or update !!! important!! return; }
Note:session.saveOrUpdate(Object o) method will check if there is an existing object in the database, if yes, update its info. If no, save the new object to the database.
Delete Customer
1. Delete Customer List
1 2 3 4
| <a href="${deleteLink}" onclick="if (!(confirm('Are you sure you want to delete this customer?'))) return false">Delete</a>
@Override publicvoiddeleteCustomer(int theId){ Session session = sessionFactory.getCurrentSession(); // delete the customer in the database Query theQuery = session.createQuery("delete from Customer where id =:theCustomerId"); theQuery.setParameter("theCustomerId", theId); theQuery.executeUpdate(); return; }
Reference: Udemy, Spring & Hibernate for Beginners (including SpringBoot)