-->
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.  [ 2 posts ] 
Author Message
 Post subject: Self referencing many-to-many
PostPosted: Tue Nov 01, 2005 12:19 pm 
Newbie

Joined: Wed Oct 12, 2005 2:50 pm
Posts: 11
I have a many-to-many which is self referencing. I need one end of the relationship to keep a list of children. This is working fine, except the list-index is not being updated when a new Category is added to the list of children. Any ideas? The relevant parts of the mapping are:

<class name="software.executiveutilities.model.Category" table="CATEGORIES" lazy="false">
<id name="id" column="ID">
<generator class="increment" />
</id>


<list name="children" table="CATEGORY_CHILDREN" lazy="false" inverse="false">
<key column="PARENT_CATEGORY_ID" not-null="true" />
<list-index column="CHILD_ORDER" base="0" />
<many-to-many column="CHILD_CATEGORY_ID" class="software.executiveutilities.model.Category" />
</list>

<join table="CATEGORY_CHILDREN" optional="true">
<key column="CHILD_CATEGORY_ID" unique="true" />
<many-to-one name="parent" column="PARENT_CATEGORY_ID" not-null="true" class="software.executiveutilities.model.Category" />
</join>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 7:55 pm 
Newbie

Joined: Wed Oct 12, 2005 2:50 pm
Posts: 11
I still can't seem to get the list-index to be updated. I have changed my approach to try and implement something like this http://www.hibernate.org/193.html even though from what I have read I should be able to do indexed collections in a bidirectional association with Hibernate 3. Using this Hibernate 2 approach my new mapping like this:

<list name="children" table="CATEGORIES" lazy="false" inverse="false" >
<key column="PARENT_ID" />
<list-index column="CHILD_ORDER" base="0" />
<one-to-many class="software.executiveutilities.model.Category" />
</list>

<many-to-one name="parent" column="PARENT_ID" class="software.executiveutilities.model.Category" />
<property name="childOrder" column="CHILD_ORDER" type="int" update="true" insert="true"/>


Now according to the documentation, this should work :

private int getChildOrder()
{
return this.getParent().getChildren().indexOf(this);
}


private void setChildOrder(int index)
{
// not used, calculated value, see getIndex() method
}


I can understand how this would work for retrieving the childOrder from a given Category, but how does this work for setting the order? When I add a new child Category to the list, the childOrder of children is still not being updated. Do I have to do these updates to Category.childOrder manually? For example, add a new Category to the list of children, then go through each of the children and set their childOrder approriately? Surely, there is a better way? This seems WAY to difficult to maintain. Any help is MUCH appreicated.


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

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.