Question:
I'm pretty new to (N)Hibernate and I'm trying to persist parts of an existing database. The database is in use, and I have no way to change the schema.
My problem is that for several tables I need to read the value of the primary key in the database, and act on it. But when I do a Get, Load, Find, etc. the id is not filled with the value from the database, but rather the UnsavedValue.
So, the question is, how do I get the value of the id in the persisted class?
Following is just one such example, there are many and more complex uses. In the example I only read the healthcare objects, never save or update them.
Hibernate version:
1.2.0.Beta2
Mapping documents:
Code:
[Serializable]
[HibernateMapping(DefaultLazy = true)]
[Class(Table = "Healthcare", NameType = typeof(Healthcare))]
public class Healthcare
{
private int mHealthcareId;
private string mDescription = null;
[...]
[Id(1, Column = "HealthcareId", TypeType = typeof(Int32), UnsavedValue = "0")]
[Generator(2, Class = "assigned")]
public virtual int HealthcareId
{
get { return mHealthcareId; }
private set { mHealthcareId = value; }
}
[Property(1, Column = "Description", NotNull = false, Length = 255)]
[Type(2, NameType = typeof(string))]
public virtual string Description
{
get { return mDescription; }
set { mDescription = value; }
}
[...]
}
Code between sessionFactory.openSession() and session.close():Code:
IList<Healthcare> healthcares = session.CreateCriteria(typeof(Healthcare)).List<Healthcare>();
foreach (Healthcare healthcare in healthcares)
{
ydelseskoder.Add(healthcare.HealthcareId.ToString(), healthcare.Description);
}