-->
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.  [ 7 posts ] 
Author Message
 Post subject: Inheritance mapping without mapping parent class.
PostPosted: Fri Oct 14, 2005 12:18 pm 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
Hibernate version: 3.0

Mapping documents:

Hello, this is my first post here and I'll try to explain what I'm looking for.

Well, First of all, I have two tables in my database, one called RangeRequest and the other one called IpRequest. To implement this situation into java source, I have this structure:

Request <- Parent
RangeRequest extends Request <-- Child
IpRequest extends Request <--Child

RangeRequest and IpRequest share attributes which I've implemented into their parent, called Request. Reading the documentation and tutorials over internet, I found some mapping strategies:

1) Using <subclass> when you want to map these 3 objects in one table.
2) Using <joined-subclass> when you want to map these 3 objects in separeted tables, one per object and
3) Using <union-subclass> that is another way to map using several files instead of putting all the mapping together in one file (correct me if i'm wrong)

Well, neither of these strategies serves to my purposes. What I'm looking for is to have 2 tables, one for RangeRequest and other for IpRequest, without needing to map parent class called Request and without having a table called Request. Do you know what I'm telling you?

I've tried to use access, import, package tags naming directly to the parent class, inside to child class, where are getters and setters, etc .. but i can't get this working.

Is there any way to do what i want or do I have to map the Request class (the parent one) which means that I have to model another table called Request into my database?

I hope that you understand my question, if this was answered befored sorry for this post, I've searched over the forum and I've not found nothing about this topic. If there is one topic about this, could you refer it to me?

Thanks in advance.

Miguel M.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 12:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You are forgetting the other inheritence mapping strategy: <class/> ;)
Just map the two things using the normal <class/> mappings.

That works simply enough as long as you do not need polymorphic associations to 'Request' (meaning all mapped associations explicitly name either 'RangeRequest' or 'IpRequest'). If you have the need to map associations to 'Request', then you need to use <any/> mappings to acheive that.


Top
 Profile  
 
 Post subject: I'm not sure about using <class/> strategy.
PostPosted: Fri Oct 14, 2005 1:07 pm 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
I guess that using <class> I have to have getters and setters inside my child classes, isn't it?
But there's a little problem with that cause childs inherit their parent attributes and that means
that their parent have implemented such methods (get and set) but childs haven't.

Oh well, lets see, this is a little piece of code:

Request {

private codRequest;
private descRequest;

{getters and setters}
...
}

RangeRequest extends Request {
private rangeSize;
private service;
private parentService;
private comments;
private user;
private managerUser;
...
{getters and setters}
...
}

IpRequest extends Request {
{own attributes like in Range Request}
...
{getters and setters}
...
}

This means that RangeRequest and IpRequest inherit their parent attributes: codRequest and descRequest, doesn't it?
Lets see what I have inside my database model:
-----------------
RangeRequestTable
-----------------
codRangeRequest
descRequest
rangeSize
service
parentService
comments
user
managerUser
...
------------------

------------------
IpRequest
------------------
codIpRequest
descIpRequest
...
------------------
But I don't have a table called request.

So, when I have to map both objects I have to specify where hibernate has to look for getter and setter for codRangeRequest
and descRangeRequest (the same for IpRequest).

Since Request doesn't have a table in my database model I can't do this:
<hibernate-mapping>
<class name="package.to.request.class.Request" table="There's no table">
<id name="codRequest" column="There's no table, so there's no column" type="int">
</id>
<property name="descRequest" column="The same as above" type="string"/>
</class>
</hibernate-mapping>

And for my child classes RangeRequest and IpRequest I have no idea how to tell hibernate where to look for getters and setters
for codXXXX and descXXXX attributes, cause they don't have implemented such methods, they are implemented in their parent.
Do you know?
I've tried to do this in RangeRequest.hbm.xml file:
<...>
<class name="package.to.rangerequest.class.RangeRequest" table="RANGE_REQUESTS">
<property name="package.to.request.class.Request.codRequest" column="COD_RANGE_REQUEST" type="int"/>
<property name="package.to.request.class.Request.descRequest" column="DESC_RANGE_REQUEST" type="string"/>
</class>
<...>
But hibernate tries to search for getters and setters inside RangeRequest class, instead of searching in Request class. This means
that hibernate doesn't find such methods and a Exception takes place.
Sorry if i'm so naive for not understand this, but I don't see the way to get this working without having to modify my database model
creating a new table called REQUESTS and with this mapping I know that there won't be problems but this is not what i'm looking for.
(If there's no another way I'll have to create the REQUESTS table of course).

Thanks again, for answering my first topic and for reading this new reply.

Miguel M.


Top
 Profile  
 
 Post subject: Re: I'm not sure about using <class/> strategy.
PostPosted: Fri Oct 14, 2005 1:18 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
If RangeRequest extends Request, and Request has getter/setter methods for the properties in your RangeRequest mapping file, hibernate will have no problem finding them. This is simple Java inheritance stuff.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:43 pm 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
This is my last post till monday, when I'll be able to prove this.
Thanks for answering to my topic. I don't forget to vote your replys ;) but let me say a last thing. Tell me if you're telling me to do this:

RangeRequest.hbm.xml
<hibernate-mapping>
<class name="package.to.RangeRequest" table="RANGE_REQUESTS">
<id name="codRequest" column="cod_range_request" type="int">
<generator>
...
</generator>
</id>
<property name="descRequest" column="desc_range_request" type="string"/>
<!-- more properties -->
</class>
</hiberante-mapping>

<hibernate-mapping>
<class name="package.to.IpRequest" table="IP_REQUESTS">
<id name="codRequest" column="cod_ip_request" type="int">
<generator>
...
</generator>
</id>
<property name="descRequest" column="desc_ip_request" type="string"/>
<!-- more properties -->
</class>
</hibernate-mapping>
[Note that name="codRequest" and name="descRequest" is the same in both mappings]
Will Hibernate look inside RangeRequest and its parent Request to search for getCodRequest and setCodRequest (the same for DescXXX and IpRequest)? It's not necessary to map Request class? (remember that request class doesn't have a table into my database model).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:46 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Lasneyx wrote:
This is my last post till monday, when I'll be able to prove this.
Thanks for answering to my topic. I don't forget to vote your replys ;) but let me say a last thing. Tell me if you're telling me to do this:

RangeRequest.hbm.xml
<hibernate-mapping>
<class name="package.to.RangeRequest" table="RANGE_REQUESTS">
<id name="codRequest" column="cod_range_request" type="int">
<generator>
...
</generator>
</id>
<property name="descRequest" column="desc_range_request" type="string"/>
<!-- more properties -->
</class>
</hiberante-mapping>

<hibernate-mapping>
<class name="package.to.IpRequest" table="IP_REQUESTS">
<id name="codRequest" column="cod_ip_request" type="int">
<generator>
...
</generator>
</id>
<property name="descRequest" column="desc_ip_request" type="string"/>
<!-- more properties -->
</class>
</hibernate-mapping>
[Note that name="codRequest" and name="descRequest" is the same in both mappings]
Will Hibernate look inside RangeRequest and its parent Request to search for getCodRequest and setCodRequest (the same for DescXXX and IpRequest)? It's not necessary to map Request class? (remember that request class doesn't have a table into my database model).


That is correct. By "extending" request, RangeRequest and IpRequest DO ACTUALLY HAVE the methods setCodRequest() and getCodRequest(). That's how inheritance works.

This of course assumes that these two methods are defined in Request.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 4:47 pm 
Newbie

Joined: Wed Oct 12, 2005 2:13 pm
Posts: 3
Ok, I think you guys just answered the question I poised earlier this week.
http://forum.hibernate.org/viewtopic.php?t=941990

(its at the end of the thread)

So, thanks!

-Vel


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