【发布时间】:2020-06-06 16:24:08
【问题描述】:
在数据库中创建对象后,我需要向其他微服务发送请求。我只发送对象 ID,因此其他微服务需要再次调用 db 以获取包含其他内容的信息。
但是,当其他微服务尝试使用接收到的 id 查找记录时,它无法在数据库中找到保存的记录。
即使@postPersist 被调用,我尝试调试似乎也不会保留记录。 它会在@PostPersist 被执行后保存。
有没有人可以为此提供解决方法。我真的需要再次查询数据库,因为这是自定义要求。我使用mysql和spring boot
public class EmployeeListener {
@PostPersist
public void sendData(Employee employee){
Long id = employee.getEmployeeId();
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("http://localhost:8081/service/employee"+id, HttpMethod.POST, null, String.class);
}
}
@Entity
@EntityListeners(EmployeeListener.class)
public class Employee {
//
}
【问题讨论】:
-
什么是 JPA 版本?
-
我使用的是 spring boot 1.5.4 版本。它使用休眠 5.0.12
标签: java database spring spring-data-jpa entitylisteners