-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: I have run into some limitations using <key-many-to-one&g
PostPosted: Sun Aug 06, 2006 9:18 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
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

_________________
/nodje


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.