I'm using SpringBoot 2.2.6
with JPA 2
and Hibernate 5
and I have the following problem.
I have a @Service
similar to following one:
public class Service {
@Autowired OtherService otherService;
@Autowired SaveService saveService;
public doIt() {
Entity entity = otherService.findById(id) // retrieve the entities
entity.getSubEntities().add(subEntity) // should add a row to certain table (subentity is a 1:n relationship)
saveService.update(entity); // the row is added, I can see it on phisical DB
entity = otherService.findById(id); // the DTO subentity doesn't contain the row added
}
}
public class OtherService {
@Autowired JpaRepository jpaRepository;
public Entity findById(Integer id) {
return jpaRepository.findById(id);
}
}
public class SaveService {
@Autowired JpaRepository jpaRepository;
public void update(Entity entity) {
jpaRepository.save(entity);
}
}
As I wrote in the comments, the first find extract an entity with a subEntity (1:n relationship) correctly loaded. The second row save a new line in the db and the third line retrieve the Entity again.
If I debugging the app, when I arrive to the third row I can see the new line added to the DB but when I fire the find the entity doesn't have the subentity (a list) correctly loaded.. It has one element less than the DB..
Why does this happen? I remove the @Transactional
annotation everywhere to avoid similar error (the order of query and so on..)
Thank you.