Hi folks,
We are facing a big problem…
We got a Java object like this : Beneficiaire contains RessourceRGAL.
public class Beneficiaire extends BaseBean
{
...
private Set<RessourceRGAL> lesRessourceRGAL = new HashSet<RessourceRGAL>();
}
Mapping hbm file is described like this :
hibernate-mapping package="bean.beneficiaire" default-lazy="true" default-cascade="none"
class optimistic-lock="version" table="beneficiaire_bene" name="Beneficiaire"
...
set name="lesRessourceRGAL" table="ressourcergal_rrga"
key foreign-key="fk_rrga_bene_1"
column name="bene_id_lesressourcergal" index="in_rrga_5" not-null="false"column
/key
one-to-many class="bean.RessourceRGAL" /
/set
When reading the collection, we restrict the results, for instance on a specific year. Thus, the collection is barely never complete.
Let’s say we’ve found just one object for the year 2018.
If we want to delete this element of the collection:
unBeneficiaire.getlesRessourceRGAL().remove(unRessourceRGAL)
and now synchronizing with the database
saveorupdate(unBeneficiaire)
Here is what Hibernate does :
update ressourcergal_rrga set bene_id_lesressourcergal=null where bene_id_lesressourcergal=?
Which means that if we had some RessourceRGAL for other years, they now have become orphans.
Same problem when finding no object at all for a year ; we have to create them with
unBeneficiaire.setLesRessourceRGAL(new HashSet());
saveorupdate(unBeneficiaire)
Hibernate is gonna put all FK to null.
Is there a way to avoid this behaviour?
Best regards,
Thomas (France)