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.  [ 11 posts ] 
Author Message
 Post subject: Inheritance and mapping files
PostPosted: Wed Apr 21, 2004 6:19 am 
Newbie

Joined: Mon Apr 19, 2004 12:55 pm
Posts: 6
Hi,

I have following classes in Java :
public class A
{
int i;
String s;
...
}
public class B extends A
{
...
}

In Hibernate documentation, I read :
Place each class mapping in its own file.
Don't use a single monolithic mapping document. Map com.eg.Foo in the file com/eg/Foo.hbm.xml. This makes particularly good sense in a team environment.

So, I will have 2 mapping files : A.hbm.xml and B.hbm.xml.
A.hbm.xml file could be:
<hibernate-mapping>
<class name="A" table="A_table">
<id column="id" type="int" unsaved-value="none">
<generator class="hilo"/>
</id>
<property name="i" type="int" column="i"/>
<property name="s" type="string" column="s"/>
</class>
</hibernate-mapping>

Here is my problem :
I don't know what to write in the B.hbm.xml file.
Moreover I must have this file because my application try to instanciate B class and hibernate configuration search for the B.hbm.xml file.

Regards,
Christophe D.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 7:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/hib_docs/refer ... tance.html

the doc isn't so big and is very well done


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 9:35 am 
Newbie

Joined: Mon Apr 19, 2004 12:55 pm
Posts: 6
I think you don't undestand what I want to do.

I can simply all write in the a.hbm.xml file :
<hibernate-mapping>
<class name="A" table="A_table">
<id column="id" type="int" unsaved-value="none">
<generator class="hilo"/>
</id>
<discriminator column="disc"/>
<property name="i" type="int" column="i"/>
<property name="s" type="string" column="s"/>

<subclass name="B">
<property name="f" type="float" column="f"/>
</subclass>
</class>
</hibernate-mapping>

But I don't want to do that.
I want to have a B.hbm.xml file too.
I would like to write something like :
<hibernate-mapping>
<class name="B">
<extends name="A" />
</class>
</hibernate-mapping>

Is it possible?

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 9:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
<hibernate-mapping>
<subclass name="B" extends="A">
</subclass>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 10:34 am 
Newbie

Joined: Mon Apr 19, 2004 12:55 pm
Posts: 6
Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Inheritance and mapping files
PostPosted: Sun Jul 19, 2009 6:39 pm 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
this solution means, that the "table per class hierarchy"-strategy is used, right?

what i want to do is the "table per concrete class"-strategy. there are some classes in a framework that get extended and there is no reason that every class that exend those framework-classes should be saved in the same table. so what i need is a pure mapping-subclassing. is that possible? respectively is that possible with the suggested solution?


Top
 Profile  
 
 Post subject: Re: Inheritance and mapping files
PostPosted: Mon Jul 20, 2009 7:39 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
ok, i found an answer. here it is:

you can do subclassing with the strategy "table per concrete class" by using union-subclass. read about it here.

if you dont need polymorphism, its better to include the mapping file xml-wise and they recommend:
Quote:
If you want to avoid duplication, consider using XML entities (for example, [ <!ENTITY allproperties SYSTEM "allproperties.xml"> ] in the DOCTYPE declaration and &allproperties; in the mapping).


how the referencing works is explained here and in this thread here.

now the question that remains, at least for me, is how does that referenced xml file looks like.


Top
 Profile  
 
 Post subject: Re: Inheritance and mapping files
PostPosted: Tue Jul 21, 2009 7:21 am 
Newbie

Joined: Mon Jul 20, 2009 7:11 am
Posts: 6
Hi there:

I posted a reply on another thread that may help you out here. The thread is viewtopic.php?f=1&t=998412&p=2415211#p2415211

Ryan


Top
 Profile  
 
 Post subject: Re: Inheritance and mapping files
PostPosted: Wed Jul 22, 2009 6:15 pm 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
thank you very much! this is the answer to my problem. much appreciated. i read your thread but unfortunatly i dont know an answer to you problem :(. im relativly new to all of this and furthermore im using mapping files, because i dont like annotations. :)

since you seem to know xml/dtd pretty well, maybe you could tell me, if there is the possibility to parameterize such an entity? i want to map generic classes. imagine in the AbstractChoice.hbm.xml described in your thead, there was the following property:
Code:
<many-to-one name="survey" column="SurveyID" class="?????"/>

the class of a survey is in the hand of a generic parameter, like
Code:
class AbstractChoice<SurveyType> {
   private SurveyType survey;
}

what i want to do is, give that entity an parameter and insert the value of the parameter there, where now are the question marks.

i already read that there are parameterized entities too and i really tried to figure out how to use them in the way that i would need to but i couldnt figure it out. maybe you can tell me if this is possible at all? and if its possible, maybe give a small example? :D because im really to dumb to figure it out, i really tried, but from what i saw (in the examples and explanations i read), i cant see any parameters in the parameterized entities. i just dont get it.

edit: one more question. :D what if there was more than one supertype? i would have to add an entity into an entity, an AbstractChoice.hbm.xml into another AbstractChoice2.hbm.xml, which was the subclass of the first mentioned AbstractChoice.hbm.xml. AbstractChoice2.hbm.xml would then get included into the final mapping file. this isnt possible is it?


Top
 Profile  
 
 Post subject: Re: Inheritance and mapping files
PostPosted: Thu Jul 23, 2009 6:29 am 
Newbie

Joined: Mon Jul 20, 2009 7:11 am
Posts: 6
Glad my posting could help.

I'm afraid I don't know of any way to do what you're asking; in my experience an entity is generally a static piece of XML. I don't think parameter entities aren't what you want; these appear to be shortcuts for defining DTDs, allowing you to specify, say, the allowable children of a particular element and then re-use that set of allowable children in multiple element definitions.

I also don't know for sure the answer to this part of your question:
Quote:
edit: one more question. :D what if there was more than one supertype? i would have to add an entity into an entity, an AbstractChoice.hbm.xml into another AbstractChoice2.hbm.xml, which was the subclass of the first mentioned AbstractChoice.hbm.xml. AbstractChoice2.hbm.xml would then get included into the final mapping file. this isnt possible is it?

However, a google search for "recursive entity processing XML" seems to indicate that if it would be possible; you'd put a reference to the "grandparent" entity in the "parent" entity using the &entity; mechanism, and then in your "child" XML mapping document, you'd have to declare the locations of both of the entities.

I hope that helps!


Top
 Profile  
 
 Post subject: Re: Inheritance and mapping files
PostPosted: Thu Jul 23, 2009 10:37 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
this helps alot! thank you very much, you gave me the information i needed.

i now see, that it isnt possible to solve the mapping-of-generic-classes problem with the help of dtd entities and that those entities arent even the final solution for non-polymorphic table-per-concrete-class inheritance. there are two reasons for this:

the first one is, that the properties-xml file (containing the part of the mapping that is to be included with dtd-entities) is just ugly and not they are not convenient. its ugly because i write plain xml with no headers, just as an txt-resource that gets included and "accidently" creates new xml-content. for me this is a hack. not convenient because i have to split every mapping-file in the hbm.xml containing the actual mapping and the ugly properties-xml file.

the second reason is and this one has more weight, if there is a class A that has a superclass S1 that in turn again has a superclass S2, then the class A has to know the superclass of S1 (that is S2) so that it can import S2's properties as enties. this is really bad and not acceptable.

my conclusion is, hibernate has no native support for non-polymorphic table-per-concrete-class inheritance and no support for mapping generic classes at all. the in the reference suggested dtd-entity solution is as explained by no means satisfactory. it is misusing a mechanism that was created for another purpose. and it doesnt solve the mapping of generic classes problem.

at least for me this is a real gap, as i want to develop a component based system. i desperatly need those two features. maybe i will try to contribute.


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