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.  [ 6 posts ] 
Author Message
 Post subject: Selecting from a subclass
PostPosted: Sun Oct 30, 2005 10:34 pm 
Newbie

Joined: Sun Oct 30, 2005 10:16 pm
Posts: 3
Sorry for the newbie question but...

I have hibernate setup with my unit tests and it works great. I have a User class which I can read with a session.createQuery("from data.User").list() statement.

What I then did is create a subclass called UserViewValue which is meant for my web tier. This subclass contains extra stuff which is just related to web pages.

In my User.hbm.xml file I want to define that subclass so I can select data from it from the web tier but I'm not sure how to configure that. When I try I get a "No discriminator found for ..." exception.

I define the subclass like this:

Code:
<subclass name="web.viewValues.UserViewValue" extends="data.User"/>


I guess I don't understand the usage of "discriminator".

Are there any runnable samples of this kind of thing?

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 11:53 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
if I understand you correctly, you don't need the subclass element. just map UserViewValue to the table you are currently mapping User to, and don't map you "extra stuff". as long as "UserViewValue extends User", you can get your data with "from UserViewValue" .

by using the subclass element, you are instructing H to treat the data model as though it exhibited inheritance, whereas you only need inheritance w/ you object model.

also, usually "from data.User" is not needed, just "from User". the former can get you into trouble when refactoring the model to a different package.


Top
 Profile  
 
 Post subject: RE: Selecting from a subclass
PostPosted: Mon Oct 31, 2005 7:39 am 
Newbie

Joined: Sun Oct 30, 2005 10:16 pm
Posts: 3
Ok thanks. That works. But what I end up with is a mapping file where I have to duplicate everything. Is there an easier way?

Code:

<hibernate-mapping>

    <class name="data.User" table="USERS">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>

        <property name="username" column="Username"/>
        <property name="password" column="Password"/>
        <property name="name" column="Name"/>
        <property name="email" column="Email"/>
        <property name="allowVisibleEmail" type="boolean" column="AllowVisibleEmail"/>


    </class>

    <class name="web.viewValues.UserViewValue" table="USERS" >

        <id name="id" column="ID">
            <generator class="increment"/>
        </id>

        <property name="username" column="Username"/>
        <property name="password" column="Password"/>
        <property name="name" column="Name"/>
        <property name="email" column="Email"/>
        <property name="allowVisibleEmail" type="boolean" column="AllowVisibleEmail"/>


    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 12:35 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
why do you need to map data.User? Hibernate will get all of the Users whenever you ask to pull all of the UserViewValues . are you using inheritance in the data model?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 1:50 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
The technical answer to your question is quite simple, but I'd like to put a few questions in front.

1. Is your inheritance structure appropriate?

Extending a data or business layer class to get a web tier class is a little bit unusual. The solution alternatives I know allow

- either to work with the business domain object directly from the web tier, in which case you wouldn't have to define another user class for the web tier,

- or transfer data between the business and web tier via data transfer objects (DTO). These DTOs are generally not defined by extending the business layer class, which would make all business layer functionality available on the web tier, but they are pure data objects tailored to the needs of a particular web view.

I don't know the web framework you're using, and your general architecture, but I suggest to check whether one of the solutions sketched above is viable for you.

In case you still need this inheritance, no matter in which tier(s), the next question is

2. Will the instances of UserViewValue be persisted?

If they aren't, don't bother to make the subclass known to Hibernate. The only benefit for defining a non-persistable class in H I can think of might be if you have a strict rule to generate all your POJOs from hbm.cfg files.

If they are, the next question is

3. Will the additional attributes of UserViewValue be persisted?

Your mapping example suggests you don't want them to be. If that's true, don't bother to make the subclass known to H, see above. If you want them to, the discriminator game has to be played, which answers your original question

4. How are subclasses mapped?
Code:
<class name="User" table="USERS"
   discriminator-value="User">
   ... id and properties as you gave them

   <discriminator column="DISC" type="string"/>

   <subclass name="UserViewValue"
      discriminator-value="WebUser">
   ... don't repeat id!
   ... don't repeat User's properties!
   ... declare additional properties
   </subclass>
</class>

You can give the discriminator column any name you want, of course. It will indicate, in the database, which Java classes the records belong to. You can choose arbitrary discriminator values, as long as they are unique.


Top
 Profile  
 
 Post subject: RE: Selecting from a subclass
PostPosted: Tue Nov 01, 2005 5:22 am 
Newbie

Joined: Sun Oct 30, 2005 10:16 pm
Posts: 3
Thanks. You're configuration works. :)

Extending a data or business layer class to get a web tier class is a little bit unusual.

I'm using struts but I'm experimenting with a different view layer. So yes this is unusual. The view layer I'm building uses pure HTML. There's no special tags or anything. In order to render a grid of data I have to bind the grid object to a list of beans. Then I use reflection to get the column values to display. In the case of a "web" bean, I need extra columns for Edit and Delete links. I might end up doing it a different way but...

I've considered using DTOs but it would be adding another iteration to convert the from one thing to another. Inheritance seems a simpler way.

Thanks again


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