Hi
Please find the mapping file contents below.
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
default-lazy="false"
assembly="NHibernateTest1"
namespace="NHibernateTest1">
<class name="UIConfiguration" table="ConfigData" >
<id name="ID" type="System.Int32" >
<column name="ID" update="false" insert="false"/>
<generator class="identity"/>
</id>
<property name="SelectedUIItems" column="SelectedUIItems" type="System.String" not-null="true" length="250"/>
</class>
</hibernate-mapping>
Here is the object representation that maps to the DB entity in above mappingCode:
public class UIConfiguration
{
private int p_ID = -1;
private string _listOfUIItems;
public UIConfiguration()
{
}
public UIConfiguration(Int32 id)
{
ID = id;
}
/// <summary>
/// The ID of the product.
/// </summary>
public virtual int ID
{
get { return p_ID; }
set { p_ID = value; }
}
public virtual string SelectedUIItems
{
get
{
return _listOfUIItems;
}
set
{
_listOfUIItems = value;
}
}
public override int GetHashCode()
{
return (GetType().FullName + "|" +
ID.ToString()).GetHashCode();
}
}
Here is the class that manages NHibernate operationsCode:
public enum SessionAction { Begin, Continue, End, BeginAndEnd }
public class PersistenceManager
{
#region Declarations
// Member variables
private ISessionFactory m_SessionFactory = null;
private ISession m_Session = null;
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public PersistenceManager()
{
this.ConfigureNHibernate();
}
#endregion
#region IDisposable Members
public void Dispose()
{
m_SessionFactory.Dispose();
}
#endregion
/// <summary>
/// Close this Persistence Manager and release all resources (connection pools, etc). It is the responsibility of the application to ensure that there are no open Sessions before calling Close().
/// </summary>
public void Close()
{
m_SessionFactory.Close();
}
/// <summary>
/// Saves an object and its persistent children.
/// </summary>
public void Save<T>(T item)
{
using (ISession session = m_SessionFactory.OpenSession())
{
using (session.BeginTransaction())
{
session.SaveOrUpdate(item);
session.Transaction.Commit();
}
}
}
/// <summary>
/// Configures NHibernate and creates a member-level session factory.
/// </summary>
private void ConfigureNHibernate()
{
// Initialize
Configuration cfg = new Configuration();
cfg.Configure();
/* Note: The AddAssembly() method requires that mappings be
* contained in hbm.xml files whose BuildAction properties
* are set to ‘Embedded Resource’. */
// Add class mappings to configuration object
Assembly thisAssembly = typeof(TaskPadConfiguration).Assembly;
cfg.AddAssembly(thisAssembly);
// Create session factory from configuration object
m_SessionFactory = cfg.BuildSessionFactory();
}
#endregion
}
Here is the client code..........
Code:
static void Main(string[] args)
{
PersistenceManager persistManager = new PersistenceManager();
UIConfiguration item = new UIConfiguration();
item.SelectedUIItems = "Item1";
persistManager.Save<UIConfiguration>(item);
}
Please let me know if you need any other information.