I have one many-to-many relationship in my model. There are two entities, Emergencies and Crews (crews can attend many emergencies, and vice versa). Code parts responsible for the relationship look as follows:
@JoinTable(name = "CREWS_ON_EMERGENCIES", joinColumns = {
@JoinColumn(name = "EMERGENCY_ID", referencedColumnName = "EMERGENCY_ID", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "CREW_ID", referencedColumnName = "CREW_ID", nullable = false)})
@ManyToMany(fetch= FetchType.EAGER)
private Collection<Crew> crewList;
and
@ManyToMany(mappedBy="crewList", fetch= FetchType.EAGER)
private Collection<Emergency> emergencyList;
Crew is a dependent entity -- an emergencyList is updated when I add new Emergency with list of crews attended.
Actually, the database is updated fine, and new records appear in the CREWS_ON_EMERGENCIES table right after the transaction is closed. But if add a new emergency and then display a list of all crews and emergencies they have attended, the last emergency will not be displayed (obviously, "emergencyList" is out of date). It will appear only when I restart a server (Tomcat) or in debug mode.
Emergency save() code:
@Transactional
public void save(Emergency e) {
entityManager.persist(e);
entityManager.flush();
}
Selected crews are added automatically in the collection crewList by Spring MVC.
Does anyone know why it happens in a way like this and what is the solution?