I have a use-case that calls for a weak association. Consider a domain object M and another domain object V. The life-cycle of these objects are completely independent. V wants to "monitor" M, but it allowable for M to be deleted while being monitored by V.
We are using JPA annotations and using Hibernate to generate our schema.
Using any kind of hibernate association between M and V results in foreign key constraints that prevent us from deleting M while V has a reference to it. We receive a foreign key constraint violation from the DB (Postgres).
I have tried just about everything, including the obvious (setting "nullable" to "true" on the @JoinColumn, trying to use an association table via @JoinTable, setting "optional" to "true" on a @OneToOne, trying a @ManyToOne....)
I have also tried to use @Formula to specify SQL, but the result set is not returned in the target object class.
I could use a @NamedQuery, but our domain objects do not have a reference to the session or session factory.
Basically I want Hibernate to fetch on object by foreign key, and if it is not there, return null.
For now, we are just storing the surrogate key in our domain object V, and then doing a query each time.... but that defeats our goal of having this managed by Hibernate.
|