-->
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.  [ 8 posts ] 
Author Message
 Post subject: XDoclet and subclasses
PostPosted: Wed Nov 17, 2004 12:26 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I have a base class called Address, which for the purposes of this example we can regard as abstract - I don't want to persist it in the db. It has all the expected methods such as getStreet, getCity, example. Now, I have some subclasses of this which I do want to persist - OrganisationAddress, CustomerAddress, etc. Using XDoclet, I would normally specify the necessary mapping above the getXXX method, e.g.:

...

/**
* @hibernate.property column="street" type="string" length="80"
*/
public String getStreet(){

...

But of course I don't actually have those methods now, they're all inherited from the parent class. So how do I specify the column mapping? Do I have to do something like the following? I.e., including the method but simply delegating to the parent:
...

/**
* @hibernate.property column="street" type="string" length="80"
*/
public String getStreet(){
return super.getStreet();
}


I hope not, as it takes a lot of the advantage of inheritance away.


...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 12:36 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I have part of my answer: the subclass inherits the properties anyway, so all I need to do is this:

/**
* @hibernate.class table="organisation_address"
*
*/
public class OrganisationAddress extends Address{

}

I must say I have had some difficulty fully understanding the Inheritance Mapping chapter of the reference on this point, and if anyone knows of any good documentation that would explain a bit more about table-per-concrete class, per hierarchy, etc., I would be eager to know about it. As it is, as I understand it simply doing what I have illustrated here does not use Hibernate's subclassing capabilities at all.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 8:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Consider trying 'Hibernate In Action'


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 2:38 pm 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
J2R wrote:
I have part of my answer: the subclass inherits the properties anyway, so all I need to do is this:

/**
* @hibernate.class table="organisation_address"
*
*/
public class OrganisationAddress extends Address{

}

I must say I have had some difficulty fully understanding the Inheritance Mapping chapter of the reference on this point, and if anyone knows of any good documentation that would explain a bit more about table-per-concrete class, per hierarchy, etc., I would be eager to know about it. As it is, as I understand it simply doing what I have illustrated here does not use Hibernate's subclassing capabilities at all.



You may look into testcases of xdoclet-2 plugin - there is an example

YOu shall map top level class as @hibernate.class , and the use @hibernate.subclass for classes sharing the same table, or @hibernate.joined-subclass for separate tables.

( See my signature for link )

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


Top
 Profile  
 
 Post subject: Multiple 'empty' subclasses, or...?
PostPosted: Sat Nov 20, 2004 2:30 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
OK, I've moved on now to using a table per class hierarchy strategy and have run into another issue. Various different classes (Customer, Organisation, etc.) have multiple addresses, which I'm implementing as Lists of Address objects. They could all just use vanilla Address objects, but I'm not able to think of a way of doing this using Hibernate, at least not with the XDoclet tags. The reason is that in the 'many' class, in this case Address, you need to specify the 'one' class in the many-to-one tag. Thus:

/**
* @hibernate.many-to-one column="address_owner_id" class="testing.Customer"
*/

I clearly can't have the same Address class work with multiple different 'parent' classes. The approach I've taken is to have multiple subclasses of Address, namely CustomerAddress, OrganisationAddress, etc., which are effectively empty in that they do not provide any different behaviour from the superclass but are really just a place to put the different Hibernate XDoclet tags. So in the base Address class, I have a getAddressOwner() method, which I override in each subclass thus (from the CustomerAddress:

package testing;

/**
@hibernate.subclass discriminator-value="Customer"
*/
public class CustomerAddress extends AbstractAddress{

/**
* @hibernate.many-to-one column="address_owner_id" class="testing.Customer"
*/
public AddressOwner getAddressOwner() {
return super.getAddressOwner();
}
}


Now I have to say that this business of creating 'empty' classes merely as places to put Hibernate tags strikes me as not quite right - I would have hoped Hibernate would not need to be so intrusive. This leads me to suppose that there must be a much better way of doing this, and any ideas are most welcome!


Top
 Profile  
 
 Post subject: Re: Multiple 'empty' subclasses, or...?
PostPosted: Sun Nov 21, 2004 8:39 am 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
J2R wrote:
OK, I've moved on now to using a table per class hierarchy strategy and have run into another issue. Various different classes (Customer, Organisation, etc.) have multiple addresses, which I'm implementing as Lists of Address objects. They could all just use vanilla Address objects, but I'm not able to think of a way of doing this using Hibernate, at least not with the XDoclet tags. The reason is that in the 'many' class, in this case Address, you need to specify the 'one' class in the many-to-one tag. Thus:

/**
* @hibernate.many-to-one column="address_owner_id" class="testing.Customer"
*/

I clearly can't have the same Address class work with multiple different 'parent' classes. The approach I've taken is to have multiple subclasses of Address, namely CustomerAddress, OrganisationAddress, etc., which are effectively empty in that they do not provide any different behaviour from the superclass but are really just a place to put the different Hibernate XDoclet tags. So in the base Address class, I have a getAddressOwner() method, which I override in each subclass thus (from the CustomerAddress:



Well, you may have to think about directions of relation. Does address need to know that it belongs to certain entity? Do you like to navigate to this entity from adress?
You may have to create class hierarchy of "adressables" too...

Quote:

Now I have to say that this business of creating 'empty' classes merely as places to put Hibernate tags strikes me as not quite right - I would have hoped Hibernate would not need to be so intrusive. This leads me to suppose that there must be a much better way of doing this, and any ideas are most welcome!


Well, hibernate is intrusive - you still have to think about identites, collection types, (sometimes ) serializability, keys etc...

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


Top
 Profile  
 
 Post subject: Re: Multiple 'empty' subclasses, or...?
PostPosted: Sun Nov 21, 2004 8:48 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
[quote="ko5tik"]Well, you may have to think about directions of relation. Does address need to know that it belongs to certain entity? Do you like to navigate to this entity from adress?
You may have to create class hierarchy of "adressables" too...[quote]

No, Address doesn't need to know that it belongs to a certain entity. Does this help me?

As regards Addressables, all the various 'parents' (Customer, Organisation, etc), implement an AddressOwner interface which I had hoped would allow me to be non-specific in my many-to-one tag (i.e., write the interface name as the class in the many-to-one tag. When I do that, though, Hibernate complains that no mapping exists for the class (which of course it doesn't, as I do not want an AddressOwner table).

I have the feeling I'm nearly there but just missing some vital piece of the jigsaw.


Top
 Profile  
 
 Post subject: Re: Multiple 'empty' subclasses, or...?
PostPosted: Mon Nov 22, 2004 6:35 am 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
Quote:
No, Address doesn't need to know that it belongs to a certain entity. Does this help me?


In this case you can try many-to-many mapping ( which uses another table ) or composite element ( but then your address is not an entity... )

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


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