I am using jpa entity listner class where and spring data repository . In @PreMethod update if I used @Transactional(propagation = Propagation.REQUIRES_NEW)
I am getting data from the database directly. If I use the same transactional annotation over private method( getOldEmployee ) being called inside @PreMethod
I am not getting the data from the database rather its using the same latest session data not sure why this behaviour is happening
Entity Class
@Entity
@Table(name = "Employee")
@EntityListeners({EmployeeListener.class)
public class Employee extends AuditEntity implements Serializable {
}
Listener class
@Component
public class EmployeeListner{
@Autowired
EmployeeRepository employeeRepository;
@PreUpdate
@Transactional(propagation = Propagation.REQUIRES_NEW) //if use here I get the employee database value before updating the database
public void preUpdateEvent(Employee employee) {
Employee employee=getOldEmployee(employee);
}
private Employee getOldEmployee(Employee employee) { //if inspite of using @Transactional(propagation = Propagation.REQUIRES_NEW) in @PreMethod if use here I am not getting the database value rather getting latest value
Optional<Employee> getOldEmployeeOptional =employeeRepository.findById(employee.getId());
getOldEmployeeOptional
}
}
Spring applies the Transactional settings only on public methods, in other case the settings are ignored.
From Spring documentation:
When using proxies, you should apply the @Transactional annotation only to methods with public visibility.
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.