-->
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.  [ 3 posts ] 
Author Message
 Post subject: Parent/child with list: please feedback!
PostPosted: Wed Feb 09, 2005 6:59 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
I need a parent child relationship (with inverse="true"..) implemented with a list; reading documentation and posts in the forum I know that:
1) list is not the canonical form of collection to map parent/child
2) someone has proposed a workaround: http://www.hibernate.org/193.html , but It seems that there are still some problems.
Starting from this, a (very) little modification:


PARENT CLASS:

public class Parent {

Long id;
Integer version;
String desc;
List childs = new ArrayList();

public void addChild(Child child) {
child.setParent(this);
childs.add(child);
}


public void delChild(int index) {
childs.remove(index);
}


// accessors and mutators omitted

}



CHILD CLASS:

public class Child {

Long id;
Integer version;
String description;
Parent parent;


public Integer getIndex() {
if (parent!=null) // for childs created but not yet added to parent...
return new Integer(this.getParent().getChilds().indexOf(this));
else
return null;
}


public void setIndex(Integer index) {
// do nothing
}


// other accessors and mutators omitted


}



PARENT MAPPING:

<class name="Parent">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<version name="version"/>
<property name="description" type="string"/>
<list name="childs" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="parent"/>
<index column="index"/>
<one-to-many class="Child"/>
</list>
</class>



CHILD MAPPING:

<class name="Child">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<version name="version"/>
<property name="description" type="string"/>
<property name="index" type="integer"/>
<many-to-one
name="parent"
column="parent"
class="Parent"
not-null="true"/>
</class>



It seems to work well, but any feedback will be appreciated.


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 09, 2005 7:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Note that this problem is completely fixed in HB3. Wait for the next release.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 09, 2005 8:21 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
gavin wrote:
Note that this problem is completely fixed in HB3. Wait for the next release.


Thanks, but... the classes and mappings I've posted are working or not?
I'm still confused...what's exactly the problem?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.