-->
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.  [ 10 posts ] 
Author Message
 Post subject: Descriminator, subclass and "Repeated column in mapping
PostPosted: Tue Mar 09, 2004 12:13 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
Using Hibernate 2.1.2

Looked at http://forum.hibernate.org/viewtopic.ph ... ass+insert and got scared .... :-)

I have a class "Service" and it has column "SERVICE_KIND" if the value of SERVICE_KIND='operstor' I would like a new class representing this. Classic example of subclass .. .I thought ....

Doing:

<class
name="com.xyz.model.Service"
table="SERVICE"
>
<id
name="companyS"
column="COMPANY_S"
type="java.lang.String"
length="19">
<generator class="com.xyz..SurrogateKeyGenerator"/>
</id>


<discriminator column="SERVICE_KIND" type="java.lang.String"/>

<property
name="companyName"
column="COMPANY_NAME"
type="java.lang.String"
length="80"
/>
<property
name="serviceKind"
column="SERVICE_KIND"
type="java.lang.String"
length="40"
/>

<!-- associations -->

<!-- subclasses -->

<subclass
name="com.xyz..model.Operator"
discriminator-value="operator">
/>

</subclass>

</class>

Gives an error alla "net.sf.hibernate.MappingException: Repeated column in mapping for class com.xyz.model.Operator should be mapped with insert="false" and update="flase": SERVICE_KIND.

Hmm -- I cannot set insert/update = flase in the SERVICE_KIND property since teh app will add new rows to "Service". Than I tought just giving the subclass a "dummy" extra property like
<property
name="serviceKind2"
column="SERVICE_KIND"
type="java.lang.String"
length="40"
insert="false"
update="false" />

giving the same error.


Am I doing somehing terrible stuoid wrong here ? One naive solution is of course to copy the whole mapping file for "Service", call it "Operator" and add a where="SERVICE_KIND='operator'" in the class block - but than this new class knows nothing of that it actual is a "Service" ... so I really would like to get the subclass to work for this.


Thanx for a great prodct by the way. Even if I am frustrated now - my experience with Hibernate is positive.

B


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 12:33 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
you can't have....

<discriminator column="SERVICE_KIND" type="java.lang.String"/>

and

<property name="serviceKind" column="SERVICE_KIND" type="java.lang.String" length="40"/>

You don't need to specify a property for your discriminator column. Hibernate is thinking you have two duplicate columns, I think.

Hope this helped. Good Luck ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 12:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, no, no. That thread talks about a completely different case! His case is that he is using the primary key as the discriminator, which is not supported.

Your case is easy: just do what the exception message says, and map the property insert="false" update="false". It will work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 12:50 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
Hey !! Way to go ! Yup - that worked -- but now - if I remove teh propety "serviceKind" and have a discriinator like

<discriminator
column="SERVICE_KIND"
type="java.lang.String"
length="40"
/>


how do I get the property "serviceKind" on the BaseClass ? Or am I never suppose to get this when I am using subclass concept .... i.e. that I never have a concrete base class ?

I.e. suppose I would like to do a select serviceKind from Service ?

Thanx so much - you helped me at least go to bed today without thinking "why th eheck does this not work ...." :-)
B


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 12:54 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
Hi gavin - ok - you'r right - differnt case.

But - the problem here is I do not want to disable the base class (Service) from insert or updates. Well, I am still a very fresh newby but the suggestion I just got form removing the property did work. Unfortunatly now the serviceKind property is gone on the BaseClass.

Thanx again
B


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 12:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So do what I told you, and what the exception itself told you.

Map the <property> insert="false" update="false". Stop arguing. Just do it. :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 1:01 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
Quote:
how do I get the property "serviceKind" on the BaseClass ? Or am I never suppose to get this when I am using subclass concept .... i.e. that I never have a concrete base class ?


Yes, you don't need it, because Hibernate is awesome and handles all this for you. You could add a discriminator attibute to your <class> tag, lets say called "service". Then if you did a from service query, hibernate would return a list of Service objects, but you can do an instance of on the Service to see if it's an Operator. Is this making sense? Lets say you just want all the Operator... from Operator would return just the Operators. Pretty awesome right?

So Hibernate is basically doing the where service_kind = 'operator' for you.

Oh yea, I almost forgot. Hibernate has a reserved word for the discriminator called "class". Check it out in the docs, they are very good and extremly helpful.

Hope this makes sense. ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 1:03 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
Here is an example from the docs, of how to use the class reserved word.

Code:
from eg.Cat cat where cat.class = eg.DomesticCat

http://www.hibernate.org/hib_docs/reference/html/query-language.html#query-language-s5


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 1:07 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
OK done it :-)


Final mapping is

<class
name="com.xyz.model.Service"
table="SERVICE"
>
<id
name="companyS"
column="COMPANY_S"
type="java.lang.String"
length="19">
<generator class="com.xyz..SurrogateKeyGenerator"/>
</id>


<discriminator
column="SERVICE_KIND"
type="java.lang.String"
length="40"
/>

<property
name="companyName"
column="COMPANY_NAME"
type="java.lang.String"
l ength="80"
/>
<property
name="serviceKind"
column="SERVICE_KIND"
type="java.lang.String"
length="40"
insert="false"
update="false"
/>

<!-- associations -->

<!-- subclasses -->

<subclass
name="com.xyz..model.Operator"
discriminator-value="operator">
/>

</subclass>

</class>



But will I now be able to do an insert/update on the "Service" class ?.. If not - how else should teh mapping be modified ?

Anyhow - thanx to all of you. Frustration went from 10 to 2 - nice ;-)

B


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 1:19 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 11:14 am
Posts: 38
Location: Houston, Texas
Bingo - tcollin !

From the documentation is nicly states:

"Likewise, the special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.
"

Thanx - problem solved.


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