Hi Andi (and others),
I tried that one before.
First I had this:
DataStrip:
Code:
@ManyToMany(
fetch = FetchType.LAZY,
targetEntity=DataStrip.class,
cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH}
)
@JoinTable(
name="datastrip_coupling",
joinColumns={@JoinColumn(name="couplinginformation_id")},
inverseJoinColumns={@JoinColumn(name="datastrip_id")}
)
public Collection<DataStrip> getDataStripList() {
return dataStripList;
}
CouplingInformation:Code:
@ManyToMany(
fetch = FetchType.LAZY,
targetEntity=CouplingInformation.class,
cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH},
mappedBy="couplingInformationList"
)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Collection<CouplingInformation> getCouplingInformationList() {
return couplingInformationList;
}
When I do next scenario:
1. 1. new DS1
2. add new CI to DS1
3. persist DS1
4. get CI-id of CI in DS1
5. get new CI with CI-id of point 4 (fresh object)
--> Everything works fine
When I do the reverse:
1. new CI
2. add new DS to CI
3. persist CI
4. get DS-id of DS in CI
5. get new DS with DS-id
--> NULL: the intermediate table was not filled in !!
So the solution I got to resolve this was:
CouplingInformation:Code:
@ManyToMany(
fetch = FetchType.LAZY,
targetEntity=CouplingInformation.class,
cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH}
)
@JoinTable(
name="datastrip_coupling",
joinColumns={@JoinColumn(name="datastrip_id")},
inverseJoinColumns={@JoinColumn(name="couplinginformation_id")}
)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Collection<CouplingInformation> getCouplingInformationList() {
return couplingInformationList;
}
Note that in both versions of CouplingInformation I did not succeed in
having size=1 of DS in a CI when a CI was persisted in a DS (scenario in first posting)
So, unless I am missing something fundamentally, this does not do the trick !