-->
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.  [ 7 posts ] 
Author Message
 Post subject: Char enumerations
PostPosted: Tue Aug 23, 2005 5:45 pm 
Newbie

Joined: Thu Oct 16, 2003 3:26 pm
Posts: 12
I realize that to .net, a char is the same as an int. But for database "readability", we wanted to store the enum's value as a char in the database. But hibernate doesn't save or read the value correctly. When it updates the row, it saves the value as '*' (an invalid value anyway) no matter what it was set as, and then throws an exception ("Input string was not in the correct format") on read because it's trying to convert '*' to an Int32.

Is this just the way it has to be, or is this considered a bug?


Top
 Profile  
 
 Post subject: Re: Char enumerations
PostPosted: Wed Aug 24, 2005 9:43 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
KarlShea wrote:
to .net, a char is the same as an int

I don't understand why you are saying that.

Can you post your mapping and the whole exception ?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 3:55 pm 
Newbie

Joined: Thu Oct 16, 2003 3:26 pm
Posts: 12
Well, they're not exactly the same, but you can't have a Char Enum. It's stored as an Int32 behind the scenes.
Code:
public enum GarageType
{
   CodeA = 'A',
   CodeD = 'D',
   Unknown = 'U'
}

is my enum, and if I do
Code:
Enum.GetUnderlyingType(typeof(GarageType))

it returns Int32.

In the mapping file, I have
Code:
<property name="GarageType" type="BuyHomes.Core.Types.GarageType, BuyHomes.Core" />

If the database column is a char(1), '*' gets saved in the column, and when I try to pull it out, an exception is thrown in PersistentEnumType.GetValue, because '*' can't be converted to an Int32, which is what it sees the Enum as.

If the database column is an int, the ASCII value of the character gets stored in the database, and everything works fine.

I was just thinking it would be nice to be able to store the character value in the database instead. If I get some time, I may modify the PersistentEnumType to support this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 7:45 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Are you sure that it depends of NHibernate ?
It looks like it is a design choice of .NET...

A workaround would be to create an array with all these chars and use the enum to use them:
Code:
char[] GarageType =  new char[] { 'A', 'D', ... }
enum GT
{
   CodeA,
   CodeD,
   Unknown
}

// Usage
Entity.MappedCharProperty = GarageType[GT.CodeD];

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 2:30 pm 
Newbie

Joined: Thu Oct 16, 2003 3:26 pm
Posts: 12
Yeah, that would work. When I get some spare time I'm going to try to get it to work in hibernate and submit a patch or something. I was just wondering if there was planned support for storing the characters. Because if I were to do
Code:
GarageType type = (GarageType) 'A'
it would work.


Top
 Profile  
 
 Post subject: Re: Char enumerations
PostPosted: Tue Oct 19, 2010 8:51 am 
Newbie

Joined: Mon Feb 22, 2010 1:35 pm
Posts: 3
Did you ever submit that patch?


Top
 Profile  
 
 Post subject: Re: Char enumerations
PostPosted: Tue Oct 19, 2010 10:02 am 
Newbie

Joined: Tue Oct 19, 2010 9:46 am
Posts: 1
Hi guys, glad to be a part of your community and hope to enjoy my stay here
__________________
montecristo cigars


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