-->
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.  [ 4 posts ] 
Author Message
 Post subject: Interface usage in hibernate
PostPosted: Thu Aug 31, 2006 7:09 am 
Newbie

Joined: Wed Aug 30, 2006 7:46 am
Posts: 4
Hello!

I have an urgend problem and hope someone can help me.

That´s the initial situation:

interface X{
...
}

class abstract A{
...
}


class abstract B{
...
}


class C extends A implements X{
...
}

class D extends B implements X{
...
}

class E {

X element;
...

}


The class E has an X element (this could be C or D). The classes C und D inherit from absolut different classes (A und B) but implement the same interface (X).

Any idea, how this could be mapped?

A solution in which the superclasses (A und B) implement the interface is not relevant. Only C and D should implement the Interface.

Many thanks!!

wl


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 4:22 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Can you include what you want your tables to look like?

It seems like you can have two tables: C and D

Just provide mappings for both C and D. The fact that they both implement the X interface does not interfere with the mapping since you are mapping "properties" of the classes, not the "behavior" that an interface adds to a class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 04, 2006 2:52 am 
Newbie

Joined: Wed Aug 30, 2006 7:46 am
Posts: 4
Hello!


First of all, thanks for your answer!

The problem is, that the class E (also a persistent class) should have a property from the interface type X (this could be C or D). So i must declare in the mapping for C and D that they implement the X-Interface.
I don´t know how i could declare this.

<class name="E">
....
<many-to-one name="element" entity-name="X">
</many-to-one>

<!-- The element could be C or D -->



</class>

<class name="C" extends="A">
....
</class>

<class name="D" extends="B">
....
</class>

Hope you can help me.

Thanks in advance.

wl


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 04, 2006 12:21 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Now I understand your issue.  I haven't done this before, but there's a special mapping tag called a discriminator that handles polymorphic persistence. Try reading this section of the reference manual: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-discriminator


5.1.6. discriminator

The <discriminator> element is required for polymorphic persistence using the table-per-class-hierarchy mapping strategy and declares a discriminator column of the table. The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row. A restricted set of types may be used: string, character, integer, byte, short, boolean, yes_no, true_false.


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