spring-projects / spring-projects/spring-data-rest
@HandleBeforeSave RepositoryEventHandler should be able to contain old entity [DATAREST-373]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Marcel Overdijk opened DATAREST-373 and commented
In case of @HandleBeforeSave it's desired to have the entity object values of the old domain object being updated.
The use cases are:
- Need to re-encrypt a password when it's updated
- send a warning email when a particular values gets bigger than a given treshold (only first time)
- ..
So it would be an idea to support something like
@HandleBeforeSave
public void handleBeforeSave(Customer oldCustomer, Customer newCustomer) { .. }
To workaround this limitation I tried something like:
@Component
@RepositoryEventHandler(Customer.class)
public class CustomerEventHandler {
private CustomerRepository customerRepository;
@Autowired
public CustomerEventHandler(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@HandleBeforeSave
public void handleBeforeSave(Customer newCustomer) {
Customer oldCustomer = customerRepository.findOne(customer.getId());
System.out.println("handleBeforeSave :: new customer.name = " + newCustomer.getName());
System.out.println("handleBeforeSave :: old customer.name = " + oldCustomer.getName());
}
}
But the problem is that the old object contains the already the new values.
So it seems the new object (with updated values) was already attached to the current (Hibernate) session.
Affects: 2.1.2 (Dijkstra SR2)
Issue Links:
- DATAREST-678
@HandleBeforeSaveseems like override jpa sessions
("is duplicated by") - DATAREST-876
@Transactionalannotation is not being enforced on repository methods
("is duplicated by") - DATAREST-874 spring-data-rest JPA repository, transaction issue
("is duplicated by")
30 votes, 31 watchers
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.