-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: discriminator type="integer"
PostPosted: Tue May 08, 2007 11:35 am 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
Hi all,

when i declare a descriminator of type integer I the error mentioned at the end of this post. When I change the type to string, the error disapears.

I suppose however it is better to give the correct type instead of always using a string ?

Anny sugestions ?



I've just updated to 1.2 GA


Thx !

Here is the mapping :

<class name ="PartType" table ="tblTemplateSheetItem">
<id column="TemplateSheetItemID" type="integer" access="nosetter.lowercase-underscore" name ="Id">
<generator class="native"/>
</id>
<discriminator column ="TemplatePartTypeID" type ="integer"></discriminator>
</class>

ERROR :
failed: Could not format discriminator value 'Bics.Common.DomainModel.TemplateManagement.PartTypeOriented' to sql string using the IType NHibernate.Type.Int32Type
NHibernate.MappingException: Could not format discriminator value 'Bics.Common.DomainModel.TemplateManagement.PartTypeOriented' to sql string using the IType NHibernate.Type.Int32Type ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
c:\net\nhibernate\nhibernate\src\NHibernate\Type\Int32Type.cs(89,0): at NHibernate.Type.Int32Type.FromStringValue(String xml)
c:\net\nhibernate\nhibernate\src\NHibernate\Type\NullableType.cs(133,0): at NHibernate.Type.NullableType.FromString(String xml)
c:\net\nhibernate\nhibernate\src\NHibernate\Type\Int32Type.cs(84,0): at NHibernate.Type.Int32Type.StringToObject(String xml)
c:\net\nhibernate\nhibernate\src\NHibernate\Persister\Entity\SingleTableEntityPersister.cs(358,0): at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)


Top
 Profile  
 
 Post subject: Use discriminator-value
PostPosted: Tue May 08, 2007 1:13 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The problem is that NH is trying to stuff the class name into an integer column. Try setting the discriminator-value of the class (and subclasses) to an integer value.


Top
 Profile  
 
 Post subject: discriminator without value
PostPosted: Thu May 10, 2007 7:44 am 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
Hi all,
I Just found out the real problem, however, I don't have a solution jet.

The problem is that I'm using a class hierarchy containing some classes that are abstract. They do not have a discriminator value.

Here is my class hierarchy :

+ PartType
- DestinationElementDescription
- PrefixElementDescription
++ PartTypeOriented
-- KeyElementDescription
-- ValidFromElementDescription
-- CostElementDescription
+++ PartTypeOrientedWithCode
---QualityElementDescription
---CalendarElementDescription
---TDEElementDescription
---TrafficTypeElementDescription
---ProductElementDescription

The once wit the +sings are subclasses which should not have a discrininator value. The one with the - do have a discriminator value.

Any Idea how to solve this ?


HERE THE COMPLETE MAPPING FILE :


<class name ="PartType" table ="tblTemplateSheetItem">
<id column="TemplateSheetItemID" type="integer" access="nosetter.lowercase-underscore" name ="Id">
<generator class="native"/>
</id>
<discriminator column ="TemplatePartTypeID" type ="string"></discriminator>
<!--property name ="PartTypeID" type="integer" column ="PartTypeID" access="field.lowercase-underscore" /-->
<many-to-one class ="Range" name ="Sheet" column ="TemplateSheetID" not-null ="true" ></many-to-one>
<bag name ="Columns" access ="nosetter.lowercase-underscore" cascade ="all-delete-orphan" inverse ="true">
<key column ="TemplateSheetItemID"></key>
<one-to-many class="Column"/>
</bag>
<subclass name ="DestinationElementDescription" discriminator-value="1"/>
<subclass name ="PrefixElementDescription" discriminator-value="2"/>

<subclass name="PartTypeOriented">
<!-- MAPS TO AN ENUMERATION-->
<property name ="Orientation" type ="Int16" column ="PartTypeOrientation" access ="field.lowercase-underscore"/>
<subclass name ="KeyElementDescription" discriminator-value="3"/>
<subclass name ="ValidFromElementDescription" discriminator-value="8"/>
<subclass name ="CostElementDescription" discriminator-value="11"/>

<subclass name ="PartTypeOrientedWithCode">
<subclass name ="QualityElementDescription" discriminator-value="5"/>
<subclass name ="CalendarElementDescription" discriminator-value="6"/>
<subclass name ="TDEElementDescription" discriminator-value ="7"/>

<subclass name ="TrafficTypeElementDescription" discriminator-value="9"/>
<subclass name ="ProductElementDescription" discriminator-value="10"/>

</subclass>

</subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 8:22 am 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
What is the value of that column when you use it as a string? Empty?

You might need to define TemplatePartTypeID as a nullable int.

Also, I don't think this is related at all.. but I always thought, probably incorrectly so, that Type had to be the CLS type... and so you'd want to define that as Int32 or System.Int32 and not 'integer'


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 10:10 am 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
I've tried a different aproach.
Instead of not providing a discriminator I provide one which will never occure (I put in negative values).
However the problem isn't solved yet. I recieve following exception :

failed: NHibernate.MappingException: Could not format discriminator value 'Bics.Common.DomainModel.TemplateManagement.PartType' to sql string using the IType NHibernate.Type.Int32Type
NHibernate.MappingException: Could not format discriminator value 'Bics.Common.DomainModel.TemplateManagement.PartType' to sql string using the IType NHibernate.Type.Int32Type ---> System.FormatException: Input string was not in a correct format.



I do not understand this exception because there is no discriminator applied on the class PartType, it is the base class.


MODIFIED MAPPING FILE

<class name ="PartType" table ="tblTemplateSheetItem">
<id column="TemplateSheetItemID" type="integer" access="nosetter.lowercase-underscore" name ="Id">
<generator class="native"/>
</id>
<discriminator column ="TemplatePartTypeID" type ="integer" ></discriminator>
<!--property name ="PartTypeID" type="integer" column ="PartTypeID" access="field.lowercase-underscore" /-->
<many-to-one class ="Range" name ="Sheet" column ="TemplateSheetID" not-null ="true" ></many-to-one>
<bag name ="Columns" access ="nosetter.lowercase-underscore" cascade ="all-delete-orphan" inverse ="true">
<key column ="TemplateSheetItemID"></key>
<one-to-many class="Column"/>
</bag>
<subclass name ="DestinationElementDescription" discriminator-value="1"/>
<subclass name ="PrefixElementDescription" discriminator-value="2"/>

<subclass name="PartTypeOriented" discriminator-value ="-1" >
<!-- MAPS TO AN ENUMERATION-->
<property name ="Orientation" type ="Int16" column ="PartTypeOrientation" access ="field.lowercase-underscore"/>

<subclass name ="KeyElementDescription" discriminator-value="3"/>
<subclass name ="ValidFromElementDescription" discriminator-value="8"/>
<subclass name ="CostElementDescription" discriminator-value="11"/>

<subclass name ="PartTypeOrientedWithCode" discriminator-value ="-2">
<subclass name ="QualityElementDescription" discriminator-value="5"/>
<subclass name ="CalendarElementDescription" discriminator-value="6"/>
<subclass name ="TDEElementDescription" discriminator-value ="7"/>

<subclass name ="TrafficTypeElementDescription" discriminator-value="9"/>
<subclass name ="ProductElementDescription" discriminator-value="10"/>

</subclass>

</subclass>
</class>



Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 10:28 am 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
FOUND THE ERROR !

I did not realize that a discriminator value shoud be specified on the class defining the discrininator.
Because I was using strings as a discriminator in the past I never noticed this because by default the class name is used as discriminator...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 12:10 pm 
Newbie

Joined: Thu May 10, 2007 12:09 pm
Posts: 1
Could you post the new mapping? I have similar issue of setting Integer discriminator-value.
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 5:14 pm 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
Sure,
apparently the trick is to put in a value which will not be present in the DB, so nHibernate will not construct this type directly

Probably something commonly know by hibernate veterans, but for me this is all new and I could not find a result to this immediately.


Here you go, my new mapping file :

<class name ="PartType" table ="tblTemplateSheetItem" discriminator-value="-1">
<id column="TemplateSheetItemID" type="integer" access="nosetter.lowercase-underscore" name ="Id">
<generator class="native"/>
</id>
<discriminator column ="TemplatePartTypeID" type ="integer" ></discriminator>
<!--property name ="PartTypeID" type="integer" column ="PartTypeID" access="field.lowercase-underscore" /-->
<many-to-one class ="Range" name ="Sheet" column ="TemplateSheetID" not-null ="true" ></many-to-one>
<bag name ="Columns" access ="nosetter.lowercase-underscore" cascade ="all-delete-orphan" inverse ="true">
<key column ="TemplateSheetItemID"></key>
<one-to-many class="Column"/>
</bag>
<subclass name ="DestinationElementDescription" discriminator-value="1"/>
<subclass name ="PrefixElementDescription" discriminator-value="2"/>

<subclass name="PartTypeOriented" discriminator-value ="-2" >
<!-- MAPS TO AN ENUMERATION-->
<property name ="Orientation" type ="Int16" column ="PartTypeOrientation" access ="field.lowercase-underscore"/>

<subclass name ="KeyElementDescription" discriminator-value="3"/>
<subclass name ="ValidFromElementDescription" discriminator-value="8"/>
<subclass name ="CostElementDescription" discriminator-value="11"/>

<subclass name ="PartTypeOrientedWithCode" discriminator-value ="-3">
<subclass name ="QualityElementDescription" discriminator-value="5"/>
<subclass name ="CalendarElementDescription" discriminator-value="6"/>
<subclass name ="TDEElementDescription" discriminator-value ="7"/>

<subclass name ="TrafficTypeElementDescription" discriminator-value="9"/>
<subclass name ="ProductElementDescription" discriminator-value="10"/>

</subclass>

</subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 2:38 pm 
Beginner
Beginner

Joined: Thu May 26, 2005 1:00 pm
Posts: 29
Probably something commonly know by hibernate veterans, but for me this is all new and I could not find a result to this immediately.

Been using NHibernate for years now, and this is the first time I've used discriminator columns, and I ran into this problem. Thanks for describing the solution.

This is NHibernate's bad design IMO. Abstract classes shouldn't need a discrim value, and even if they do, there is no way NHibernate should just default it to the class name if sub or superclasses provide explicit values; this is typically a sign of programmer error.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 4:22 pm 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
Hi,
I'm glad my post is useful for somebody.

If you know the behaviour it does not bother me. The problem is that this behaviour is not mentioned CLEARLY in the documentation.

I bought the book "hibernate in action" and I could never find a description of this kind of situations although it does not seem that exotic.

If nHibernate wants to be successful in the future, it needs clear documentation. I think a lot of newcomers from the .net front are jumping in. To catch them, clear doc's are key to success.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 12:36 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
gITs wrote:
If you know the behaviour it does not bother me. The problem is that this behaviour is not mentioned CLEARLY in the documentation. ... If nHibernate wants to be successful in the future, it needs clear documentation.


True. You should submit an addendum to the 'discriminator' section to JIRA.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 2:25 pm 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
Could you tell me how I could do it ? Then I can add it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 3:58 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
gITs wrote:
Could you tell me how I could do it ? Then I can add it.


Sure! Check out http://jira.nhibernate.org/. I think your forums login will work there. You can create a new issue. Try to reference every point in the documentation where the given addendum/erratum is needed.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 10:17 am 
Newbie

Joined: Mon Jun 04, 2007 12:26 pm
Posts: 13
Location: Houston, Texas
I was having the same problem, also being a newbie. Thanks for posting your solution.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 6:37 pm 
Newbie

Joined: Tue May 08, 2007 11:25 am
Posts: 15
Great if it has been usefull for somebody !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.