'How to recognize an update to OneToMany-relation done on the many side?
I've got a person-taxcase one to many relation:
in Person-class:
@OneToMany(mappedBy = "person")
private List<TaxCaseEntity> taxCases = new ArrayList<>();
TaxCase-class:
@ManyToOne(targetEntity = PersonEntity.class)
private PersonEntity person;
. Person can have multiple tax cases. When I create a new tax case like this:
@Test
@Transactional
public void test() {
UUID personId = UUID.randomUUID();
PersonEntity personEntity = createPersonEntity(mandantId);
personRepository.save(personEntity);
TaxCaseEntity taxCase = createTaxCase();
taxCase.setPerson(personEntity);
taxCaseRepository.save(taxCase);
PersonEntity foundEntity = personRepository.findById(personId).get();
assertThat(foundEntity.getId()).isEqualTo(personId);
}
Found entity has zero tax cases. However, when I create it like this:
PersonEntity personEntity = createPersonEntity(UUID.fromString("05e710ea-f8a4-40ba-b67c-8a685f35fc84"));
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
em.persist(personEntity);
em.getTransaction().commit();
TaxCaseEntity taxCaseEntity = createTaxEntity()
.setPerson(personEntity);
em.getTransaction().begin();
em.persist(taxCaseEntity);
em.getTransaction().commit();
em.close();
The subsequent call to personRepository.findById(..) yields a person entity instance with one tax case in the tax case list.
The question is - how can I make sure that the find-Method yields a person entity instance with the taxcase (the first snippet?) Do I have to use explicit commits or some force reloading?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
