I have to deal with cyclic dependent relations I cannot influence and I am fairly new to JPA.
So a Entity has members of the same Entity and I resolved that by:
@Entity
@Table("A")
public class A {
@ManyToMany
@JoinTable(name = "A_HAS_SUBAS",
joinColumns = {@JoinColumn(name = "A_ID")},
inverseJoinColumns = {@JoinColumn(name = "SUBA_ID")})
private Set<A> as;
}
When writing to the DB I have the problem that Hibernate seems to not know which A has to be persisted first. I tried to solve this by removing all relations from A, write to the DB and restore relations afterwards through hibernate.
This seems to work, but seems to fail if an A
has no SubAs
and this doesn't fit with my understanding of the issue. So I certainly be wrong somewhere.
The Entity without relations is persisted by an inner transaction:
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
private void immediatelySaveNewEntity(A entity) {
try {
if (!dao.entityExistsFromId((int) entity.getId())) { dao.save(entity); }
} catch (Exception e) {
e.printStackTrace();
}
}
As a result I get a
ORA-02291: integrity constraint (...) violated - parent key not found
I can circumvent this issue by removing constraints from the DB, but this is not my preferred way of dealing with this.