-->
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: Parent-Child Self Reference Mapping with 2nd Table
PostPosted: Wed Jan 21, 2009 4:23 pm 
Newbie

Joined: Wed Jan 21, 2009 3:13 pm
Posts: 5
Location: New York, NY
Hi,

I'm a beginning Hibernate user and have been struggling with an issue for a few days now that I can't seem to get past. I have 2 tables in question that are part of a legacy database:

Code:
industry
    - industry_id
    - ... more columns

industry_hier
    - industry_child
    - industry_parent
    - rank


The database is very poorly written but, for reasons outside the scope of this post, I can't change the structure (!!!!). I want to create a Parent/Child relationship and have gotten so far as to implement the children list. I can't figure out how to correctly map the parentIndustry field. Here's my code:

Industry.java:
Code:
public class Industry {
    private Integer industryId;
    private IndustryHier parentIndustry;
    private List<IndustryHier> childIndustries = new ArrayList<IndustryHier>();

    ... getters and setters ...
}


Industry.hbm.xml:
Code:
<hibernate-mapping>
    <class name="Industry" table="industry">
        <id name="industryId" column="industry_id">
            <generator class="increment"/>
        </id>
        <bag name="childIndustries" table="industry_hier" cascase="all-delete-orphan" inverse="true">
            <key column="industry_parent"/>
            <one-to-many class="IndustryHier"/>
        </bag>
    </class>
</hibernate-mapping>


IndustryHier.java
Code:
public class IndustryHier {
    private Industry parentIndustry;
    private Industry childIndustry;
    private Integer rank;

    ... getters and setters ...
}


IndustryHier.hbm.xml
Code:
<hibernate-mapping>
    <class name="IndustryHier" table="industry_hier">
        <composite-id>
            <key-many-to-one name="childIndustry" column="industry_child"/>
        </composite-id>
        <one-to-one name="parentIndustry"/>
        <property name="rank" column="rank"/>
    </class>
</hibernate-mapping>


I've tried many mappings in Industry.hbm.xml including (but not limited to):

Code:
<many-to-one name="parentIndustry" column="industry_id" unique="true" property-ref="parentIndustry" insert="false" update="false"/>

<one-to-one name="parentIndustry" property-ref="parentIndustry" class="IndustryHier" cascade="all"/>

<join table="industry_hier" optional="true" inverse="true">
    <key column="industry_child"/>
    <many-to-one name="parentIndustry" column="industry_parent" class="IndustryHier" unique="true"/>
</join>


When using these mappings Hibernate performs an update to the industry table instead of an insert (Exception: Batch update returned unexpected row count from update). Google hasn't helped me with this issue and it's gotten to the point where I'm just throwing every mapping I can find in there. Could anyone provide some assistance?


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.