/web-customer-tracker

1. Configuration for Spring + Hibernate

  1. Define database dataSource / connection pool

  2. Setup Hibernate session factory

  3. Setup Hibernate transaction manager

  4. Enable configuration of transactional annotations

2. Welcome File

  1. Server will look for a welcome file

  2. If it doesn't find one, then you'll get 404

  3. Welcome files are configured in web.xml

3. Refactor: @GetMapping & @PostMapping

3.1 @GetMapping

@RequestMapping(path="/processForm", method=RequestMethod.GET)
public String processForm(...){
...
}
  • This mapping Only handles GET method

  • Any other HTTP REQUEST method will get rejected

@GetMapping("/processForm")
public String processForm(...){
...
}
  • New annotation: @GetMapping

  • This mapping only handles GET method

  • Any other HTTP REQUEST method will get rejected

3.2 @PostMapping

@RequestMapping(path="/processForm", method=RequestMethod.POST)
public String processForm(...){
...
}
  • This mapping Only handles POST method

  • Any other HTTP REQUEST method will get rejected

@PostMapping("/processForm")
public String processForm(...){
...
}
  • New annotation: @PostMapping

  • This mapping only handles Post method

  • Any other HTTP REQUEST method will get rejected

Which One? Both can send data

4.1 Purpose of Service Layer

  • Service Facade design pattern

  • Intermediate layer for custom business logic

  • Integrate data from multiple sources(DAO/repositories)

  • @Service applied to Service implementations

  • Spring will automatically register the Service implementation

    • Thanks to component-scanning

4.3 step-by-step

  1. Define Service interface

  2. Define Service implementation: Inject the CustomerDAO

5.1 Spring @Transactional

  • Spring provides an @Transactional annotation

  • Automagically begin and end a transaction for your Hibernate code

    • No need for you to explicitly do this in your code(no need for session.beginTransaction(), session.getTransaction().commit()).

  • This Spring magic happens behind the scenes to handles transaction management.

5.2 @Repository

use for DAO Implementations.

6.Hibernate Session method

https://www.journaldev.com/3481/hibernate-session-merge-vs-update-save-saveorupdate-persist-example

6.1 save

As the method name suggests, hibernate save()

can be used to save entity to database. We can invoke this method outside a transaction, that’s why I don’t like this method to save data. If we use this without transaction and we have cascading between entities, then only the primary entity gets saved unless we flush the session.

6.2 update

Hibernate update should be used where we know that we are only updating the entity information. This operation adds the entity object to persistent context and further changes are tracked and saved when transaction is committed.

6.3 saveOrUpdate

If(primaryKey/id) empty then INSERT new customer else UPDATA exiting customer

6.4 persist

Hibernate persist is similar to save (with transaction) and it adds the entity object to the persistent context, so any further changes are tracked. If the object properties are changed before the transaction is committed or session is flushed, it will also be saved into database.

Second difference is that we can usepersist()method only within the boundary of a transaction, so it’s safe and takes care of any cascaded objects.

Finally, persist doesn’t return anything so we need to use the persisted object to get the generated identifier value.

6.5 merge

Hibernate merge can be used to update existing values, however this method create a copy from the passed entity object and return it. The returned object is part of persistent context and tracked for any changes, passed object is not tracked. This is the major difference with merge() from all other methods.

6.6 Cascade VS Inverse

In case of many-to-many relation through intermediary table; "Cascade" says whether a record will be created/updated in the child table. Whereas "Inverse" says whether a record will be created/updated in the intermediary table.

6.7 persistent context

Last updated