-->
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.  [ 12 posts ] 
Author Message
 Post subject: When and How to use <subclass>?
PostPosted: Thu Feb 05, 2004 2:03 am 
Regular
Regular

Joined: Wed Dec 03, 2003 9:41 pm
Posts: 87
I read online docs chapter 5.1. Mapping declaration. I do not know when and how to use subclass. In the example of this chapter, I have some questions.
If I add <subclass> tag in <class> tag, must put a tag <discriminator>?
In <discriminator>, column="subclass", "subclass" is a column name exist in db table?
<subclass name="DomesticCat" discriminator-value="D">
Does it mean when value of column "subclass" is 'D', this record should be a class DomesticCat?


Top
 Profile  
 
 Post subject: Yes
PostPosted: Thu Feb 05, 2004 5:07 am 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
Basically your direction is correct. I'm not at my normal machine so I can't give a simple example, as reference, but you are on the right track. I find it helpful to write a simple Test class that performs queries (or use Hibern8ide) and try out my mappings that way. You may want to try this too. Also there's the validate-hbm ant target in the middlegen examples that uses Ant's xml validation tag... it doesn't tell you what goes where (e.g., column name vs. property name) but does tell you if the mapping is at least well formed.

HTH.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:09 am 
Regular
Regular

Joined: Wed Dec 03, 2003 9:41 pm
Posts: 87
Online docs only shows all kinds of functions but not detail. If there is a tutorial including hbm.xml, table structure, persistent class and sql being created at runtime to show how to use and why to use these functions, It would be great helpful to user.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 8:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Are you sure no internal neither external tutorial covers this ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: When and How to use <subclass>?
PostPosted: Thu Feb 05, 2004 8:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
fafnir wrote:
If I add <subclass> tag in <class> tag, must put a tag <discriminator>?

Yes

fafnir wrote:
In <discriminator>, column="subclass", "subclass" is a column name exist in db table?

Yes
fafnir wrote:
<subclass name="DomesticCat" discriminator-value="D">
Does it mean when value of column "subclass" is 'D', this record should be a class DomesticCat?

Yes

You guy have got it, ready to write a tutorial ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 4:42 pm 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
Here is a sample mapping that uses a subclass. This is pretty well covered in the tutorial (I thought so at least). Another thing that is worth noting is whether polymorphism is set "implicit" or "explicit". There are good posts on the forum about that subject.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
<hibernate-mapping>
  <!-- CVS: $Id: DynamicEnum.hbm.xml,v 1.7 2004/02/04 23:42:47 eepstein Exp $ -->

<class
    name="com.publishworks.common.DynamicEnum"
    table="dynamic_enum"
    schema="common"
    discriminator-value="not null"
    dynamic-update="true"                   
    dynamic-insert="true"
    optimistic-lock="version"
>
    <meta attribute="class-description">
       Contains enum-like objects stored in the database.
    </meta>
   <meta attribute="extends" inherit="false">com.publishworks.PWBase</meta>

    <id
        name="pkId"
        type="int"
        column="pk_id"
    >
        <generator class="sequence">
            <param name="sequence">common.pw_seq</param>
        </generator>
    </id>
   
    <discriminator
        column="enum_type"
    />

    <version
        name="version"
        column="update_cnt"
    />

    <property
        name="value"
        type="int"
        column="value"
        not-null="true"
        length="4"
    >
        <meta attribute="field-description">
           The value for this enum (of type enum_type).  Values are unique within their enum_type.
        </meta>
        <meta attribute="use-in-tostring">true</meta>
    </property>

    <property
        name="name"
        type="java.lang.String"
        column="name"
        length="256"
    >
        <meta attribute="field-description">
           The name for this enum (of type enum_type).  Names are unique within their enum_type.
        </meta>
        <meta attribute="use-in-tostring">true</meta>
    </property>
    <property
        name="displayName"
        type="java.lang.String"
        column="display_name"
        length="256"
    >
        <meta attribute="field-description">
           A display name for this enum, if needed.  Often the name is enough.
        </meta>
        <meta attribute="use-in-tostring">true</meta>
    </property>
    <property
        name="sortOrder"
        type="int"
        column="sort_order"
        length="4"
    >
        <meta attribute="field-description">
           For queries and displays.
        </meta>
    </property>
   
    <!-- associations -->

   <subclass
        name="com.publishworks.production.OutputType"                             
        discriminator-value="output_type"     
        lazy="true"                             
        dynamic-update="true"
        dynamic-insert="true">
      <!-- consider using net.sf.hibernate.PersistentEnum; -->
   </subclass>


</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 10:12 pm 
Regular
Regular

Joined: Wed Dec 03, 2003 9:41 pm
Posts: 87
I did a simple test program to learn this function but it throws exception.
The following is my xml and source code.
<hibernate-mapping>
<class name="h2test.test.Book" table="Book">
<id column="book_id" name="id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<discriminator column="has_discount" type="short"/>
<property column="book_name" length="50" name="bookName" type="java.lang.String"/>
<property column="put_date" length="23" name="putDate" type="java.util.Date"/>
<property column="price" length="19" name="price" type="java.lang.Double"/>
<subclass name="h2test.test.DiscountBook" discriminator-value="0"
dynamic-insert="true" dynamic-update="true">
<property name="discount" column="discount" type="java.lang.Double"/>
</subclass>
</class>
</hibernate-mapping>

Source code fragment:
Session session = super.currentSession();
Criteria crit = session.createCriteria(Book.class);
crit.add(Example.create(book));
return crit.list();

exception:
building session factory
net.sf.hibernate.MappingException: Could not format discriminator value to SQL string

at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:783)

at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)

at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:136)

at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:720)

What is my error?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:26 am 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
Just a guess... it might be because you didn't specify a descriminator value on the base (super) class. The descriminator you are using is of type short and the default way that hibernate discriminates classes is by using the class name itself if you do not supply a discriminator value. So it is probably trying to convert the class name to a short or vice-versa.

Try specifying a discriminator for the base class. E.g., "not null"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 5:13 am 
Regular
Regular

Joined: Wed Dec 03, 2003 9:41 pm
Posts: 87
You are right, eepstein.
Thank you.


Top
 Profile  
 
 Post subject: When and How to use <subclass>?
PostPosted: Sat Feb 21, 2004 3:27 pm 
Newbie

Joined: Sat Feb 21, 2004 3:02 pm
Posts: 14
My issue is that I want the properties to be interfaces but would like Hibernate to create an implementing subclass. The docs say that I should declare my table as implementing the imterface and the implementing class as a subclass. This suggests that there is no discriminator - all instances are of the subclass but I cannot find a good example of how this works
Imagine com.matrix.bo.IDog is an interface and
com.matrix.bo.DogImpl implements com.matrix.bo.IDog
then I say
<class
name="com.matrix.bo.IDog"
table="Dog"
>

...
<subclass
name="com.matrix.bo.DogImpl"
discriminator-value="????" ???Do I need this?????
???What do I Use?????
>
</subclass>
</class>

_________________
Steven M. Lewis PhD
4221 105th Ave NE
Kirkland, WA 98033
425-889-2694
206-384-1340 (cell)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2004 3:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
no, no, no! There is no reason to declare the IDog interface!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2004 7:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There are quite a bunch of examples on how to map something like that here http://www.hibernate.org/hib_docs/reference/html/inheritance.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.