views:

6537

answers:

5

Hi,

Is there a way to persist an enum to the DB using NHibernate? That is have a table of both the code and the name of each value in the enum.

I want to keep the enum without an entity, but still have a foreign key (the int representation of the enum) from all other referencing entities to the enum's table.

A: 

As far as I know, this is not possible by default. However, Google brings up several interesting results on NHibernate Enum. One of the ways I would probably try out is Oran Dennison's Generic NHibernate Enum String Mapping, or, if you can modify the Enum, Persisiting Described Enums as described on NHibernate Forge.

hangy
Thanks, but this are all referring to the string of the enum, where I want to have a foreign key to the int representation itself.
Meidan Alon
+2  A: 

An easy but not so beautiful solution:

Create an integer field with and set the mapping in the mapping file to the field. Create a public property that uses the integer field.

private int myField;
public virtual MyEnum MyProperty
{
   get { return (MyEnum)myField; }
   set { myField = value; }
}
Paco
+8  A: 

You can use the enum type directly: http://graysmatter.codivation.com/post/Justice-Grays-NHibernate-War-Stories-Dont-Use-Int-If-You-Mean-Enum.aspx. If your underlying type is a string, it should use the string representation, if it is numeric, it will just use the numeric representation.

But your question wording sounds like you're looking for something different, not quite an enum. It seems that you want a lookup table without creating a separate entity class. I don't think this can be done without creating a separate entity class though.

Garo Yeriazarian
That link doesn't seem to work. This one should: http://graysmatter.codivation.com/post/Justice-Grays-NHibernate-War-Stories-Dont-Use-Int-If-You-Mean-Enum.aspx
UpTheCreek
Thanks, updated linky link.
Garo Yeriazarian
Still not working for me.
AlfeG
A: 

Try using a stategy pattern. Uou can then put logic into your inner classes. I use this quite alot espically when there is logic that should be contained in the "enum". For example the code below has the abstract IsReadyForSubmission() which is then implemented in each of the nested subclasses (only one shown). HTH

[Serializable]
public abstract partial class TimesheetStatus : IHasIdentity<int>
{
        public static readonly TimesheetStatus NotEntered = new NotEnteredTimesheetStatus();
        public static readonly TimesheetStatus Draft = new DraftTimesheetStatus();
        public static readonly TimesheetStatus Submitted = new SubmittedTimesheetStatus();
        //etc

        public abstract int Id { get; protected set; }
        public abstract string Description { get; protected set; }
        public abstract bool IsReadyForSubmission();

        protected class NotEnteredTimesheetStatus: TimesheetStatus
        {
            private const string DESCRIPTION = "NotEntered";
            private const int ID = 0;
            public override int Id
            {
                get { return ID; }
                protected set { if (value != ID)throw new InvalidOperationException("ID for NotEnteredTimesheetStatus must be " + ID); }
            }

             public override string Description
            {
                get { return DESCRIPTION; }
                protected set { if (value != DESCRIPTION)throw new InvalidOperationException("The description for NotEnteredTimesheetStatus must be " + DESCRIPTION); }
            }
            public override bool IsReadyForSubmission()
            {
                return false;
            }

        }
        //etc
}
RhysC
+31  A: 

Why are you guys over complicating this? It is really simple.

The mapping looks like this:

<property name="OrganizationType"></property>

The model property looks like this:

public virtual OrganizationTypes OrganizationType { get; set; }

The Enum looks like this:

public enum OrganizationTypes
{
    NonProfit = 1,
    ForProfit = 2
}

NHibernate will automatically figure it all out. Why type more than you need????

eibrahim
I'm not that familiar with Hibernate so I can't understand you post. Can you explain what the "model property" is. Where is this defined?
Alex Worden
I don't understand why your answer didn't get voted up tons more. You supply the answer in clear code that is easy to understand.
Kevin Albrecht
This does not work in nHibernate 2.1 as it causes ghosting: http://stackoverflow.com/questions/3247188/nhibernate-updates-unchaged-records
brainimus