-->
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: Hibernate, inheritance, interfaces and such
PostPosted: Fri Feb 27, 2004 6:42 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
Hello

I'm working on a system which will have a couple of classes which will all inherit a interfaces. I wanted to have some classes inherit multipel interfaces, but I can't figure out how to do it.

The only thing I think might work is adding multiple extends tags to the <joined-subclass> tag.

Right now I have something like this:

<class name="interface1">
<joined-subclass name="ClassImpl1" />
</class>

now if I want ClassImpl1 to inherit from interface2 too without having interface1 inheriting interface2, can I do something like this:

<class name="interface1" />
<joined-subclass name="ClassImpl1">
<key name="key1">
<extends name="interface1">
<extends name="interface2">
</joined-subclass>

Or do I need to find another way (possibly using the <any> construct (which I not yet fully understand)?

Jesse Houwing


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 6:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Why do you feel you have to explicitly map the interfaces?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 7:49 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
I re-read the manual and it seems that it is not really needed as you said. The only thing I can't work out is how I can best define mappings from other types to the interface without making a class reference.

It seems one can use the any tag, but I don't really get how it is best used in this context.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 6:22 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
Ok, I've looked a bit further, but I'm not sure I'm going the right way.

Let me explain the situation a bit better.

I have a number of classes that all implement their own interface to improve interoperability. Additionally they implement a number of different interfaces for security, auditing and other uses.

The relationships between classes is always done using the specific interfaces. How do I model the following as a hibernate mapping:

Interface: Securable

Interface: Class1Interface
Interface: Class2Interface

Class: Class1impl, implements Securable, Class1Interface
Class: Class2impl, implements Class2Interface

with class1 having a member pointing to an instance of class 2 using it's public interface class2interface.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 11:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Depends on what "persona" of class-2 you want exposed in class-1 (i like to think of interfaces as different personas assumed by the class). Regardless, this is all in your java code. In your hibernate mapping, only mapped entities can be referenced.

Typically, what I do is to code my java domain entities to the various interfaces. Then map the appropriate impls implementing those interfaces in the hibernate mappings.

So, for example, if your mapping for Class1impl contains a reference to Class2impl, it will work out as long as the actual property on the java class Class1impl is defined to be of type Class2impl, or Class2Interface. At that point it does not matter; its simple java assignment casting at that point.

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 5:58 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
Ok I understand and I tried this before but could not get it to work.

I mapped CustomerIimpl to hibernate with all it's properties (implements Securable & Customer)
I mapped ProjectImpl to hibernate with all it's properties (implements Securable & Customer)

there is a one to many reference from customer to project.

Generating schema from that mapping file gives the following error
Code:
D:\ontw\s-g\scripts\build.xml:336: Schema text failed: Association references unmapped class: nl.s.g.d.i
nterfaces.Project


I tried making class1impl a subclass of it's interface, this works but only for one interface. Now if I want to make a reference to a Securable it won't work.

So somehow I'll have to map Securable to the mapping file too. I've read the section in the manual concering the different ways this can be mapped, and it seems that the last options is the most appropriate for my solution, but I tried the any tag in all it shapes and sizes, but I always get errors when trying to compile the mapping document.

I tried creating additional class mappings for the interfaces, but this results in extra tables with or duplicate data fields (if I also map those fields) or just a table with id's which is not what I'm looking for I think.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 6:47 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
Another thing I just tried is to only map the interfaces. This will generate a valid schema file, but I'm not sure what this will do with polimorphic classes. Somehow it feels wrong.

I've re-read the documentation several times now, but I just can't fgure out how to map this "the right way" (if there is one), if at all...

Maybe I need more coffee.... (or tea in my case, as I don't drink coffee, maybe that's the problem)...


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.