Montag, 15. April 2013

Parent-aware unidirectional @OneToMany relationship in Hibernate/JPA

In a classical unidirectional one-to-many relation, the referred entity (Call) is not aware of its parent (Bill).
public class Bill { (...) @OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "billId") private Set calls; (...) }
Though, there may be some cases, where it is reasonable to make the information of the relation also available in the child entity (again, Call), e.g. if you want to retrieve all Calls of a specific Bill. An easy way to achieve that, is to explicetily add a @JoinColumn to the child entity. This column is completely managed by JPA/Hibernate, hence not modifieable.
public class Call { (...) @Column( insertable = false, updatable = false ) private Long billId; (...) }
This solution expects, that Id column of Bill is of type long. Use orphanRemoval=true, to force removal of not associated childs.

Keine Kommentare:

Kommentar veröffentlichen