Background:
NHibernate Version: 1.0.2.0
DB: Sql Server 2000
Problem:
I have a table in which an enumerated list of values reside; tablename is "statusList". This table contains an identity column called "statusId" and a single, unique string column named "statusName". This table is represented by the following class:
Code:
public class StatusClass {
public int ID
{
get { return _id; }
set { _id = value; }
}
private int _id;
public StatusCode Status
{
get { return _status; }
set { _status = value; }
}
private StatusCode _status;
}
The StatusCode type is an enumeration. In order to map the string values in the statusName column, I have created a StatusType class that inherits from EnumStringType:
Code:
public class StatusType : EnumStringType
{
public StatusType()
: base(typeof(StatusCode), 50) {}
}
I then defined the mappings for this class to:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="AssemblyName.StatusClass, AssemblyName" table="statusList">
<id name="ID" type="int" column="statusId">
<generator class="identity" />
</id>
<property name="Status" column="statusName"
type="AssemblyName.StatusType, AssemblyName" />
</class>
</hibernate-mapping>
Now, the issue is in the many side of this where a statusId is assigned to an entity. So, let's say the entity is defined in an entity table. This table contains an entityId identity column, entityName string column, and statusId column that holds a foreign key to my statusList table. The object representing this then looks like:
Code:
public class Entity
{
public int ID
{
get { return _id; }
set { _id = value; }
}
private int _id;
public String Name
{
get { return _name; }
set { _name = value; }
}
private String _name;
public StatusClass Status
{
get { return _status; }
set { _status = value; }
}
private StatusClass _status;
}
And, the mapping file looks like:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="AssemblyName.Entity, AssemblyName" table="entity">
<id name="ID" type="int" column="entityId">
<generator class="identity" />
</id>
<property name="Name" column="entityName" type="string" />
<many-to-one name="Status" class="AssemblyName.StatusClass, AssemblyName"
column="statusId" />
</class>
</hibernate-mapping>
Now, when I get an entity from the database, the Status property contains a Status property that represents the appropriate StatusCode enumeration value. If, however, I update that StatusCode value and save the entity back, hibernate does not change the foreign key on the entity record. Instead, it changes the string value in the statusList table where that statusId resides.
So, if the entity record is:
entityId: 1
entityName: name
statusId: 1
And, the statusList table has (in the form of statusId, statusName on each line):
1, On
2, Off
If I do:
Code:
...
entityObj.Status.Status = StatusCode.Off;
session.save(entityObj);
...
the entity record does not change (still has 1 for a statusId), however the statusList table now looks like (assuming no unique name constraint):
1, Off
2, Off
How do I do this so that the statusId value on the entity record updates to "2" instead of updating the foreign table's value??? Thanks in advance for any help you may be able to provide.