-->
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: Can I store the enum member name instead of the value?
PostPosted: Fri May 05, 2006 8:09 am 
Newbie

Joined: Wed Apr 26, 2006 5:28 am
Posts: 18
Hibernate version: 1.0.2

I have an old database which contains a column (Status) this column can have the values "New", "Assigned" or "Fixed".

I don't like to create a property which is a string. I'd like to create a enum
for example
Code:
public enum StatusType
{
   New,
   Assigned,
   Fixed
}


But when I save the object it doesn't save "New" to the table it saves 0.
Is there any possibility to say hibernate it should store the "Name" of the enum member and not the value?

Thanks for help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 8:33 am 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
Try creating a Status property like this

Code:
private StatusType m_status;

public StatusType Status
{
   get
   {
      return Enum.GetName(typeof(StatusType), m_status);
   }
   set
   {
      m_status = (StatusType)Enum.Parse(typeof(StatusType), value);
   }
}

_________________
Mike Abraham


Top
 Profile  
 
 Post subject: Re: Can I store the enum member name instead of the value?
PostPosted: Fri May 05, 2006 3:12 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
NCC-1701-M wrote:
I have an old database which contains a column (Status) this column can have the values "New", "Assigned" or "Fixed".

I don't like to create a property which is a string. I'd like to create a enum


Instead of trying to expose a string property in your class, you might be better off doing something like this.

Create a new type to use in your mapping files. You only use this type in your mapping files, your business objects continue to use the Enum only.

Code:
using NHibernate.Type;

public class StatusTypeEnum : EnumStringType
{
  public StatusTypeEnum()
    : base(typeof(StatusType))
  { }
}


Once you have StatusTypeEnum class, you then use that in your mapping files like this:

Code:
<property name="Status" column="status" type="YourNameSpace.StatusTypeEnum, YourAssemblyName" />



The benefit to this is, you only have to create the StatusTypeEnum class once, and you can use it in all of your mappings.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue May 09, 2006 1:10 am 
Newbie

Joined: Wed Apr 26, 2006 5:28 am
Posts: 18
Thank you for your responses.

I guess I'll try the second suggestion.

Greetings
-NCC


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