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: how to use discriminator right? table per subclass hierarchy
PostPosted: Tue May 12, 2009 7:22 pm 
Newbie

Joined: Wed Jan 21, 2009 4:26 pm
Posts: 12
How to use a discrimator right?

I have a stranger problem with using discriminators.
I was reading that you have to additionaly set the where-clause to the right type (discriminator value).

The problem is here that many-to-one does not provide the where-clause!!!

I was searching on the internet and was reading this article:
http://jroller.com/eyallupu/entry/getti ... en_mapping

Setting Forcing on true did not either help.

Code:
<class="A" abstract="true" table="A">
        <id name="A_ID">  .......  </id>
        <discriminator
           column="Typ"
           type="string"
           length="1"
           not-null="false"
           force="true"           
        />
       
        <property name="name" type="string" />           
               
        <subclass name="SA" extends="A" discriminator-value="S" >
        </subclass>

       <subclass name="LA" extends="A" discriminator-value="L" >
        </subclass>
</class>



Code:
<class name="B" table="B">
          <id name="B_Id"> </id>
          <!-- some stuff here -->

          <idbag name="lcList" table="A_B" cascade="all, delete-orphan" >
           <collection-id type="integer" column="A_B_ID">
            <generator class="identity" />
         </collection-id>
           <key column="B_Id" />           
           <composite-element class="myObj">
              <property name="level" column="level" type="string" length="1" not-null="false" />
              <many-to-one name="la" class="LA" column="A_ID"/>
           </composite-element>           
        </idbag>
</class>


Last edited by mahone9 on Wed May 13, 2009 5:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: how to use discriminator right? table per subclass hierarchy
PostPosted: Tue May 12, 2009 8:07 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Here's a working mapping file on a project of mine that works fine:

Code:
    <class name="jobprep.domain.User" table="user_account" abstract="true">
        <id name="id" column="user_account_id" type="long">
            <generator class="sequence">
                <param name="sequence">user_account_id_sequence</param>
            </generator>
        </id>
        <discriminator column="user_type" type="string"/>
        <property name="firstName" column="first_name"/>
        <property name="lastName" column="last_name"/>
        <property name="emailAddress" column="email_address"/>
        <property name="username" column="username"/>
        <property name="password" column="password"/>

        <subclass name="jobprep.domain.Student" discriminator-value="STUDENT">
            <bag name="activeModules" inverse="true" cascade="none" lazy="true">
                <key column="active_module_id" not-null="true"/>
                <one-to-many class="jobprep.domain.ActiveModule"/>
            </bag>
        </subclass>

        <subclass name="jobprep.domain.Teacher" discriminator-value="TEACHER">
            <many-to-one name="educationFacility" class="jobprep.domain.EducationFacility"
                         column="education_facility_id"/>
            <bag name="students" inverse="true" cascade="none" lazy="true">
                <key column="user_account_id" not-null="true"/>
                <one-to-many class="jobprep.domain.Student"/>
            </bag>
        </subclass>

        <subclass name="jobprep.domain.Admin" discriminator-value="ADMIN">
            <bag name="modules" inverse="true" cascade="none" lazy="true">
                <key column="module_id" not-null="true"/>
                <one-to-many class="jobprep.domain.Module"/>
            </bag>
            <bag name="educationFacilities" inverse="true" cascade="none" lazy="true">
                <key column="education_facility_id" not-null="true"/>
                <one-to-many class="jobprep.domain.EducationFacility"/>
            </bag>
        </subclass>
    </class>


I hope that helps.


Top
 Profile  
 
 Post subject: Re: how to use discriminator right? table per subclass hierarchy
PostPosted: Wed May 13, 2009 5:06 am 
Newbie

Joined: Wed Jan 21, 2009 4:26 pm
Posts: 12
Thanks for your reply.
I guess I try to explain my problem on your mapping file.

Let assume you have 3 tables which are related as n:m relation.

Student 1<-->n StudentSkill n<-->1 Skill

Code:
<class name="AbstractSkill" table="Skill" abstract="true">
        <id name="id" column="Skill_id" type="long">
            <generator class="sequence">
                <param name="sequence">Skill_id_sequence</param>
            </generator>
        </id>
        <discriminator column="skill_type" type="string" not-null="false" force="true"/>
        <property name="name" />
        <property name="description" />

        <subclass name="SoftSkill" discriminator-value="SoftSkill">
            <!--some properties -->
        </subclass>

        <subclass name="HardSkill" discriminator-value="HardSkill">
            <!--some properties -->
        </subclass>
</class>


<class name="jobprep.domain.User" table="user_account" abstract="true">
        <id name="id" column="user_account_id" type="long">
            <generator class="sequence">
                <param name="sequence">user_account_id_sequence</param>
            </generator>
        </id>
        <discriminator column="user_type" type="string"/>
        <property name="firstName" column="first_name"/>
        <property name="lastName" column="last_name"/>
        <property name="emailAddress" column="email_address"/>
        <property name="username" column="username"/>
        <property name="password" column="password"/>

        <subclass name="jobprep.domain.Student" discriminator-value="STUDENT">
            <bag name="activeModules" inverse="true" cascade="none" lazy="true">
                <key column="active_module_id" not-null="true"/>
                <one-to-many class="jobprep.domain.ActiveModule"/>
            </bag>

           [color=#408040]
           <idbag name="softSkills" table="StudentSkill" cascade="all, delete-orphan" >
                 <collection-id type="integer" column="StudentSkill_ID">
                       <generator class="identity" />
                 </collection-id>
                 <key column="user_account_id" />           
                 <composite-element class="StudentSkill">
                        <property name="level" column="level" type="string" length="1" not-null="false" />
                        <many-to-one name="skill" class="SoftSkill" column="Skill_ID"/>
                  </composite-element>           
             </idbag>
            [/color]

           [color=#408040]
           <idbag name="hardSkills" table="StudentSkill" cascade="all, delete-orphan" >
                 <collection-id type="integer" column="StudentSkill_ID">
                       <generator class="identity" />
                 </collection-id>
                 <key column="user_account_id" />           
                 <composite-element class="StudentSkill">
                        <property name="level" column="level" type="string" length="1" not-null="false" />
                        <many-to-one name="skill" class="HardSkill" column="Skill_ID"/>
                  </composite-element>           
             </idbag>
            [/color]


        </subclass>

        <subclass name="jobprep.domain.Teacher" discriminator-value="TEACHER">
            <many-to-one name="educationFacility" class="jobprep.domain.EducationFacility"
                         column="education_facility_id"/>
            <bag name="students" inverse="true" cascade="none" lazy="true">
                <key column="user_account_id" not-null="true"/>
                <one-to-many class="jobprep.domain.Student"/>
            </bag>
        </subclass>

        <subclass name="jobprep.domain.Admin" discriminator-value="ADMIN">
            <bag name="modules" inverse="true" cascade="none" lazy="true">
                <key column="module_id" not-null="true"/>
                <one-to-many class="jobprep.domain.Module"/>
            </bag>
            <bag name="educationFacilities" inverse="true" cascade="none" lazy="true">
                <key column="education_facility_id" not-null="true"/>
                <one-to-many class="jobprep.domain.EducationFacility"/>
            </bag>
        </subclass>
    </class>



Top
 Profile  
 
 Post subject: Re: how to use discriminator right? table per subclass hierarchy
PostPosted: Fri Jun 12, 2009 3:26 am 
Newbie

Joined: Thu Jun 11, 2009 4:32 pm
Posts: 1
hi

i have exactly the same problem as initially posted. as pointed out by the original author of the thread
Code:
<discriminator force="true" ...>
did not resolve the problem.

does anyone has a solution for that? any help would greatly appreciated...

the only workaround i found is adding manually a where clause with the discriminator, which is a bit ugly, but working. e.g.:
Code:
<list name="x" cascade="all" lazy="false"  where="type='DESC_VALUE'" > ... </list>


./patrick heusser


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.