-->
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.  [ 5 posts ] 
Author Message
 Post subject: meaning of discriminator-value attribute of class tag
PostPosted: Tue May 13, 2008 1:13 pm 
Newbie

Joined: Thu May 08, 2008 2:16 pm
Posts: 3
Hi,

I'm a beginner of Hibernate. Could anyone provide me an example of how discriminator-value attribute work? I didn't get it from online tutorial. Thanks for help.

_________________
Yijia (Allen) Zhao

Research In Motion
Software Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 4:06 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Try the manual:

http://www.hibernate.org/hib_docs/v3/re ... criminator

"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."

You won't need to use this until you map subclasses to a single table. Discriminator value lets Hibernate know which type a given row is.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject: Here are some tips
PostPosted: Tue May 13, 2008 6:25 pm 
Newbie

Joined: Tue May 13, 2008 10:01 am
Posts: 8
Following is a description that is been taken (extract) from Java Persistence book. Credit goes to the authors. I just worded a different similar example.

This is a case that I know where it can be used. Suppose we have two (it can be as many as possible) classes (that has some common/general properties) in Java and one table to represent this in database. I mean we would like to persist both the objects into a single table.

Example scenario:

Code:
public abstract class Attachment {
  - String name;
  - ETC
}

public ImageAttachement extends Attachment {
  - Integer imageSizeWidth;
  - Integer imageSizeHeight;
  - String imageType;
}

public TextAttachment extends Attachment {
  - Integer size;
}


We have only one table (to represent this class hierarchy) called
attachment (attachment_id number, type varchar(1), name varchar(25), image_size_width number, image_size_height number, image_type varchar(3), size number)

Hibernate mapping:
Code:
<class
        name="Attachment"
        table="attachment">
        <id
            name="id"
            column="attachment_id"
            type="long">
            <generator class="native"/>
        </id>
        <discriminator
            column="attachment_type"
            type="string"/>
        <property
            name="sentBy"
            column="sent_by"
            type="string"/>
            ...
        <subclass
            name="ImageAttachment"
            discriminator-value="IMG">
            <property name="imageSizeWidth" column="image_size_width"/>
            <property name="imageSizeHeight" column="image_size_height"/>
            <property name="imageType" column="image_type"/>
        </subclass>

        <subclass
            name="TextAttachment"
            discriminator-value="TXT">
        ...

    </class>



Since we are storing two objects into a single table, for persistence and retrieval purposes, since Hibernate handles this automatically and internally, we have to have a special column in the table to distinguish between these two persistent classes: the discriminator.

Please note that there is NO such property in the persistent classes. This discriminator is used internally by Hibernate. Hibernate will set correct values ('IMG" or "TXT") for this discriminator column automatically.

Hope this helps.

_________________
Best wishes


Top
 Profile  
 
 Post subject: Few more additional words
PostPosted: Tue May 13, 2008 6:30 pm 
Newbie

Joined: Tue May 13, 2008 10:01 am
Posts: 8
Mentioning of this discriminator starts from the hibernate mapping file and there is a corresponding column in the table. One can see why we need this. This is all is necessary since Hibernate does retrieval, persistence, internally.

So mostly this is to help Hibernate in correctly mapping the two classes into one table.

_________________
Best wishes


Top
 Profile  
 
 Post subject: Few more additional words
PostPosted: Tue May 13, 2008 6:30 pm 
Newbie

Joined: Tue May 13, 2008 10:01 am
Posts: 8
Mentioning of this discriminator starts from the hibernate mapping file and there is a corresponding column in the table. One can see why we need this. This is all is necessary since Hibernate does retrieval, persistence, internally.

So mostly this is to help Hibernate in correctly mapping the two classes into one table.

There is no mention of it in the Java classes.

_________________
Best wishes


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