I'm using this recommended mapping
Code:
<class name="Child">
<composite-id>
<key-property name="parentId" column="parent_id"/>
<key-property name="childId" column="child_id"/>
</composite-id>
<many-to-one name="parent" column="parent_id" insert="false" update="false"/>
...
</class>
Details:
Code:
<class name="com.xxx.model.District" table="district" schema="public">
<id name="id" type="long">
<column name="district_id"/>
<generator class="sequence">
<param name="sequence">xxx_guid</param>
</generator>
</id>
<!--a district should have exactly one detail per language supported by the app-->
<map name="details" table="district_detail" inverse="false" cascade="all,delete-orphan" fetch="join" >
<cache usage="read-write"/>
<key>
<column name="district_detail_district_id_fk" not-null="true"/>
</key>
<map-key column="district_detail_locale" type="locale"/>
<one-to-many class="com.xxx.model.DistrictDetail"/>
</map>
</class>
<class name="com.xxx.model.DistrictDetail" table="district_detail" schema="public">
<composite-id name="id" class="com.xxx.model.DistrictDetailId">
<key-property name="districtDetailDistrictIdFk" type="long">
<column name="district_detail_district_id_fk"/>
</key-property>
<key-property name="districtDetailLocale" type="locale">
<column name="district_detail_locale"/>
</key-property>
</composite-id>
<many-to-one name="district" class="com.xxx.model.District" update="false" insert="false" fetch="join">
<column name="district_detail_district_id_fk" not-null="true"/>
</many-to-one>
<property name="districtDetailName" type="string">
<column name="district_detail_name" not-null="true"/>
</property>
</class>
It works just fine except that I have to create and save a District first to be able to create DistrictDetail childs.
the following code:
Code:
District district = new District();
DistrictDetail detailZh = new DistrictDetail();
detailZh.setId(new DistrictDetailId(district.getId(), Locale.PRC));
detailZh.setDistrictDetailName("东城");
DistrictDetail detailEn = new DistrictDetail();
detailEn.setId(new DistrictDetailId(district.getId(), Locale.ENGLISH));
detailEn.setDistrictDetailName("Dongcheng");
district.addDetails(detailZh);
district.addDetails(detailEn);
mgr.saveEntity(district);
---part of District.java:
public void addDetails(DistrictDetail detail) {
detail.setDistrict(this);
this.getDetails().put(detail.getId().getDistrictDetailLocale(), detail);
}
does not work because Hibernate is trying to save DistricDetail with a DistrictDetailId that have a FK to DistrictId (districtDetailDistrictIdFk) being null (district.getId()). This is also due to the fact that I'm using a sequence for ids.
That would of course work if I'd first save District then attached the detail child and persist. But that wouldn't be the magic I expect from Hibernate.
Does anyone knows the good way to do this? Am I doing something wrong? I've looked in the forum, couldn't find any solution. <key-many-to-one> is definitely not the way to go according to the documentation, I gave up with this.
Hibernate version: 3.2CR2
Name and version of the database you are using:PostgreSQL 8.1.4