-->
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: Mapping an interface hierarchy with subclasses
PostPosted: Thu Dec 11, 2003 7:15 pm 
Newbie

Joined: Mon Dec 08, 2003 1:44 pm
Posts: 16
Location: Germany
Hello... i have a problem on how to map the following simple example scenario.

Image

The Hibernate Reference tells me to use the <subclass>-Tag to map implementig classes of interfaces. But I also need the <subclass>-Tag to express the polymorphic structure, that is i. e. "Question" is extended by "NumericQuestion"

Is the following mapping correct?

Code:
<hibernate mapping>
    <class name="Question" table="QUESTIONS" discriminator-value="Question">
        <id name="Id" column="ID">
            <generator class="native" />
        </id>
        <property name="questionText" column="QUESTION_TEXT" />
        <discriminator column="CLASS_NAME" />

        <!-- Now the implementing class -->
        <subclass name="QuestionDTO" discriminator-value="QuestionDTO" />

        <!-- Now the subinterface -->
        <subclass name="NumericQuestion" discriminator-value="NumericQuestion">
            <property name="minValue" column="MIN_VALUE" />
            <property name="maxValue" column="MAX_VALUE" />

            <!-- Now the implementig class of this interface -->
            <subclass name="NumericQuestionDTO" discriminator-value="NumericQuestionDTO" />

        </subclass>
    </class>
</hibernate-mapping>


I am concerned about the lines of code in which the DTOs (stands for "Data Transfer Object") are mapped. It's just one line, where I say what's the name of the implementing class and what discriminator-value I give to it. Is that enough?

I know that later in the running application, its the DTO-Objects, that will be persisted to the database, because you can't create an object of an interface, right? But I'd like the type of the DTOs to be the corresponding interface-type, because one should only access theses DTOs through their interfaces.

How does Hibernate understand, that in one case the <subclass>-Tag is used to declare an implementing class and in another case to declare a real subclass (subinterface in this case)?

For all help best thanks in advance!

Philipp Hinnah


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 7:03 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you save a real object ie a DTO, Hibernate will know that DTO is a DTO getClass().

If you request 'from Interface', hibernate will only find real DTO impl in discriminator column, so DTO will be instanciated

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 7:42 am 
Newbie

Joined: Mon Dec 08, 2003 1:44 pm
Posts: 16
Location: Germany
Hello epbernard!

I did not really understand, what you mean with "request 'from interface'".

Philipp Hinnah


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 8:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
in a HQL query, you can do 'from my.Interface' and it will return object id DB implementing this interface since you mapped it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 9:01 am 
Newbie

Joined: Mon Dec 08, 2003 1:44 pm
Posts: 16
Location: Germany
Aha...!

So when I specifiy the interface in the from-clause the query will return an Object ID of a DTO-Object, that is implementing this interface?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 9:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Something like that.
It will return the discriminator value and all needed properties, instanciate the appropriate implementation then filling it in

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 12:02 pm 
Newbie

Joined: Mon Dec 08, 2003 1:44 pm
Posts: 16
Location: Germany
I found another approach how to solve this problem, that I have two parallel subclass-hierarchies (one with the interfaces and one with the DTOs):

I just map the DTOs and use the interfaces as proxies! Then it'll work fine I guess :-)

P. Hinnah


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.