Hibernate version:
1.2.0 GA
Session Factory Code
Code:
public class SessionSource
{
#region private members
private bool initialized = false;
private ISessionFactory factory;
#endregion
#region public properties
/// <summary>
/// Returns true if the SessionSource has been initialized successfully.
/// </summary>
public bool IsInitialized
{
get { return initialized; }
}
#endregion
#region singleton implementation
private static SessionSource current = new SessionSource();
public static SessionSource Current
{
get { return current; }
}
#endregion
private SessionSource()
{
//prevent direct creation
}
/// <summary>
/// Reads the NHibernate configuration and builds the session factory.
/// </summary>
public void Initialize()
{
Configuration config = new Configuration();
config.AddAssembly("SocksCDS");
factory = config.BuildSessionFactory();
initialized = true;
}
/// <summary>
/// Closes the current session factory. The next call
/// to initialize or GetSession will re-initialize the SessionSource
/// </summary>
public void Close()
{
if (factory == null)
return;
factory.Close();
initialized = false;
}
/// <summary>
/// Gets an open NHibernate ISession for work with the database.
/// </summary>
/// <returns>ISession</returns>
public ISession GetSession()
{
//if we aren't initialized, do it
if (! initialized)
Initialize();
return factory.OpenSession();
}
/// <summary>
/// Gets an open NHibernate ISession (using the existing connection) for work with the database
/// </summary>
/// <param name="connection">An existing IDbConnection</param>
/// <returns>ISession</returns>
public ISession GetSession(IDbConnection connection)
{
//if we aren't initialized, do it
if (!initialized)
Initialize();
return factory.OpenSession(connection);
}
/// <summary>
/// Gets an open NHibernate ISession for work with the database.
/// </summary>
/// <param name="interceptor">A custom interceptor to attach to the session before returning it.</param>
/// <returns></returns>
public ISession GetSession(IInterceptor interceptor)
{
if (!initialized)
Initialize();
return factory.OpenSession(interceptor);
}
}
Code between sessionFactory.openSession() and session.close():Code:
ISession session = SessionSource.Current.GetSession();
ITransaction transaction = session.BeginTransaction();
Client newClient = new Client();
newClient.PhoneNumber = txtClientPhoneNumber.Text;
newClient.Sex = txtClientSex.Text;
newClient.State = txtClientState.Text;
newClient.Zipcode = txtClientZipCode.Text;
newClient.LastName = txtClientLastName.Text;
newClient.FirstName = txtClientFirstName.Text;
newClient.Street = txtClientStreet.Text;
newClient.Clientssn = txtClientSSN.Text;
try
{
session.Save(newClient);
txtClientID.Text = newClient.Clientid.ToString();
transaction.Commit();
toolStripStatusLabel1.Text = "Client Information Saved";
}
catch (Exception ex) {
Logging.LogMessageToFile(ex.ToString());
transaction.Rollback();
MessageBox.Show("An Error has occured while attempting this operation. Changes aborted. /n The Error has been logged in log.txt. /n /n Please contact the developer.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripStatusLabel1.Text = "Task Failed";
}
session.Close();
Full stack trace of any exception that occurs:
NHibernate.MappingException was unhandled
Message="Could not compile the mapping document: SocksCDS.Client.hbm.xml"
Source="NHibernate"
StackTrace:
at NHibernate.Cfg.Configuration.LogAndThrow(MappingException me)
at NHibernate.Cfg.Configuration.AddValidatedDocument(XmlDocument doc, String name)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlTextReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
at NHibernate.Cfg.Configuration.AddResources(Assembly assembly, IList resources, Boolean skipOrdering)
at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly, Boolean skipOrdering)
at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
at SocksCDS.SessionSource.Initialize() in C:\Code Projects\New Socks CDS\Socks CDS\SessionSource.cs:line 51
at SocksCDS.SessionSource.GetSession() in C:\Code Projects\New Socks CDS\Socks CDS\SessionSource.cs:line 79
at SocksCDS.MainForm.btnSaveClientInformation_Click(Object sender, EventArgs e) in C:\Code Projects\New Socks CDS\Socks CDS\MainForm.cs:line 151
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SocksCDS.Program.Main() in C:\Code Projects\New Socks CDS\Socks CDS\Program.cs:line 18
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The innerexception was 'Dialect was not set'.
Name and version of the database you are using:
Access 2003/Jet 4.0
I'm sure I'm missing something here, I just can't find the error.