-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to store an enumeration?
PostPosted: Wed Aug 31, 2005 12:49 am 
I have an Order class that uses a simple enumeration:

Code:
    public enum OrderStatus
    {
        CREATED,
        MODIFIED,
        SUBMITTED,
        CLOSED
    }

    public class Order
    {
        private OrderStatus status;
        ...
    }


What is the recommended approach to save this enumeration using NHibernate?

- What does the mapping file look like?
- What is the column type in the database? Should the enumerations be stored as numbers of strings?

Thanks.


Top
  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 4:33 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Read:
Char enumerations
Enumeration Bug?

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 2:03 am 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
Thanks. After reading these posts it appears that it is easier to store enums as numbers in the database, which is fine with me. Couple of questions:

1) I tried the following two mappings. They both seem to work fine and there is no change in behavior. Is this intentional? (perhaps because enums are the same as Int32 in .NET). Which one is the preferred way?

Code:
<property name="Status" column="Status" type="TimeTrackerDomain.TimecardStatus, TimeTrackerDomain" />

<property name="Status" column="Status" type="Int32" />



2) The "Hibernate in Action" book talks about storing enums as VARCHARs (page 209 - "Using enumerated types"). Is this approach supported in NHibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 5:40 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
1) IMO, you should use the first mapping (or you can omit it and NHibernate will use reflection to find it)

2) I don't know how Java/Hibernate handle enums, but I don't think that NHibernate support that.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 4:42 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
You can use the NHibernate.Type.EnumStringType class to store your enumerations as VARCHAR or CHAR. This is an abstract class which you can easily subclass.

From the SDK docs, if you have the following enum
Code:
public enum MyEnum
{
    On,
    Off,
    Dimmed
}

then you subclass EnumStringType as follows
Code:
public class MyEnumStringType : NHibernate.Type.EnumStringType
{
    public MyEnumStringType()
        : base( typeof( MyEnum ) )
    {
    }
}

And use a mapping like
Code:
...
    <property name="Status" type="MyEnumStringType, AssemblyContaining" />
...


This will map to a VARCHAR column in the database, and use the enum member names ("On", "Off", "Dimmed" in the example) instead of the numeric values.


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