-->
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.  [ 4 posts ] 
Author Message
 Post subject: Abstract parent/child mapping
PostPosted: Thu Feb 23, 2006 6:06 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
Hibernate version: 3.1.2

I'm having a serious problem in the mapping of a parent child structure where both parent and child are abstract classes that are derived for differente implementation.
This is the case:


abstract parent and child:

public abstract class Parent {

Long id;
// some properties omitted
List childs = new ArrayList();

// getters/setters omitted

}


public abstract class Child {

Long id;
// some properties omitted
Integer index; // index for ArrayList
Parent parent;

// getters/setters omitted

}



concrete parent and child:

public class Parent1 extends Parent {

// some new properties omitted
// getters/setters for new properties omitted

}

public class Child1 extends Child {

// some new properties omitted
// getters/setters for new properties omitted

}



In the project there are many other classes parent and child that should inherit from Parent and Child; some of these inheriting directly from Parent and Child, other will extends Parent1 and Child1.

For this reason, I would like to centralize the mappings definition at the most possible in Parent.hbm and Child.hbm and extend for other classes.

For the inheritance strategy, I will prefer table-per-concrete-class. Absolutely no table-per-subclass.


My first idea was something like this:


mapping for abstract parent and child:


<hibernate-mapping package="xxx.abstract">

<class name="Parent" abstract="true">

<id name="id" column="id" type="long">
<generator class="sequence">
<param name="sequence">parent_id_seq</param>
</generator>
</id>

<version name="version"/>

<!-- properties omitted -->

<list name="childs" inverse="true">
<key column="parent"/>
<list-index column="index"/>
<one-to-many class="Child"/>
</list>

</class>

</hibernate-mapping>



<hibernate-mapping package="xxx.abstract">

<class name="Child" abstract="true">

<id name="id" column="id" type="long">
<generator class="sequence">
<param name="sequence">child_id_seq</param>
</generator>
</id>

<version name="version"/>

<!-- properties omitted -->

<property name="index" type="integer"/>

<many-to-one
name="parent"
class="Parent"
column="parent"
not-null="true"/>

</class>


</hibernate-mapping>



and for concrete classes:


<hibernate-mapping package="xxx.concrete">

<union-subclass name="Parent1" table="parent1" extends="xxx.abstract.Parent">

<!-- new properties omitted -->

</union-subclass>


</hibernate-mapping>


<hibernate-mapping package="xxx.concrete">

<union-subclass name="Child1" table="child1" extends="xxx.abstract.Child">

<!-- new properties omitted -->

</union-subclass>


</hibernate-mapping>



This mapping is correctly loaded but, at save time, only parent is saved and no errors raised.

I REALLY, REALLY appreciate a suggestion.



Davide


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:42 am 
Beginner
Beginner

Joined: Thu Feb 16, 2006 9:53 am
Posts: 24
Could you post the code used to create and save your objects, please?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 12:37 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
alexism wrote:
Could you post the code used to create and save your objects, please?


Sorry! Forget a cascade="all" in <list>

But I'm still testing the possibilities of extension of this mapping...

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 12:40 pm 
Beginner
Beginner

Joined: Thu Feb 16, 2006 9:53 am
Posts: 24
I was suspecting something like that...


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