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?