I assume that the FCalendar owns/controls the relationship, so that when the insert statement occurs the foriegn key is set correctly. As long as Competition is in the cache, it will show the stale null reference. This means that if you are not going to set the Competition back pointer in the entity, you will need to refresh the entity after the transaction commits to have the data reflect in the shared cache, and will need to clear other entityManagers that may already have it loaded. I would suggest you find the existing Calendar rather than create it, and set the backpointer on it:
FCalendar caldb = em.find(FCalendar.class,cal.getFCalendarId()); caldb.getCompetition();
Competition comp = em.find("Comp111", Competition.class); .... FCalendar cal =new FCalendar("Cal111"); //"Cal111" is the identifier of this calendar object. cal.setCompetition(comp); comp.setCal(cal); comp.setFCalendar(cal);
(where cal is a FCalendar with id= Cal111 which is linked to a competition with id. Comp111. The DB contains a competition with such id).
em.getTransaction().begin(); em.persist(cal); FCalendar caldb = em.find(FCalendar.class,cal.getFCalendarId()); Competition compdb = caldb.getCompetition(); em.getTransaction().commit();
gets the calendar caldb from the DB and hence, caldb has not the link to Comp111 because the calendar with that link is in the cache (due to em.persist(cal); ) and will not be in the DB until commit is done???
FCalendar caldb = em.find(FCalendar.class,cal.getFCalendarId());