-->
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.  [ 10 posts ] 
Author Message
 Post subject: Mapping with interfaces
PostPosted: Fri Nov 19, 2004 12:19 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I have a system where a number of different classes (Organisation, Customer, etc), all have multiple Addresses. My original design for this involved having subclasses of Address, such as OrganisationAddress, CustomerAddress, etc., with one-to-many mappings. Each subclass would have the a field consisting of the 'parent' type (Customer, Organisation), with appropriate getter/setters. The subclasses were mapped using the table-per-class-hierarchy strategy.

Now, it struck me that this was perhaps redundant as there is no differentiation between the subclasses other than what their 'parent' is. So, I came up with the idea of an AddressOwner interface, to be implemented by Customer, Organisation, etc - all the classes which 'own' a number of addresses. These classes are all subclasses of a BaseBusinessObject with getId and setId methods, all using Longs for ids. So, why could I not simply use Address objects and have an AddressOwner object as a field of the Address, with getAddressOwner/setAddressOwner methods?

The fly in the ointment with this approach is when I come to do the mappings for the Address class. I have to specify a class for the many-to-one mapping for the AddressOwner field, but of course, AddressOwner is an interface, not a concrete class. I want Hibernate to use the concrete implementation (Customer, Organisation).

Where have I gone wrong in my thinking?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:46 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
You can simply specify the interface in your mapping files.

Code:
<many-to-one name="bla" class="myInterface"/>


If you use HibernateTools you can map an interfaces like this:
Code:
   <class name="nl.dennie.vikie.model.BBO">
      <meta attribute="interface">true</meta>
      <id>
         <meta attribute="gen-property">false</meta>
         <generator class="assigned"/>
      </id>
   </class>


Hope this helps.
[/code]

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:56 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
[quote="Dencel"]You can simply specify the interface in your mapping files.

Code:
<many-to-one name="bla" class="myInterface"/>


That's what I tried to do. When I do a build, though, I get the following error:

"An association from the table address refers to an unmapped class: mypackage.AddressOwner"

I clearly have to specify a class which has a mapping. But I don't want to have a mapping for the IAddressOwner, because I don't want to have an AddressOwner table, that would defeat the object. As I understand it, as I want my various implementations of the AddressOwner interface (Customer, Organisation) to have their own tables, and therefore I am using the table-per-concrete-class strategy, I should be getting implicit polymorphism, shouldn't I?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:02 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Map the interface. In my previous post there is an example how to do this.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:15 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I don't have Hibernate Tools. How would what you have shown look in a normal mapping file?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:20 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Sorry, I should be more accurate. This mapping doesn't require HibernateTools.
This mapping will work in Hibernate. Hibernatetools looks at the extra meta-attributes, but they are ignored by Hibernate.
In the example BBO is an interface.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:31 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
OK, thanks. I'm using XDoclet for my Hibernate tagging, so I'm assuming that there is something I can put at the head of the class which will generate the mapping you mention. I would normally put something like this, specifying a table:

/**
* @hibernate.class table="customer"
*
*/

But does it make sense to do simply this, as I don't want a table?

/**
* @hibernate.class
*
*/


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:36 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
I don't use XDoclet. It's probably documented in their documentation. I rather start from mapping file instead of java files when programming with hibernate. But that is mho.
The workings of XDoclet is off topic here.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:45 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
OK, I'll ask the question differently. What sense does a class mapping without a table attribute make? I'm trying to understand what it is doing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 25, 2004 2:42 pm 
Beginner
Beginner

Joined: Sun Feb 08, 2004 4:09 pm
Posts: 46
Hi,


I use xdoclet and mapped the interface as a normal concrete class, although, if you use schema-export, it will create the table for the interface that hibernate will never use (i suppose, running through tests, nothing was inserted)

Best regards,
thiago souza


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