使用NHibernate过程中 出现的奇怪问题
本人刚接触NHibernate 做了一个小demo 代码如下
private void Form1_Load(object sender, EventArgs e) { Configuration cfg = new Configuration().Configure(); ISessionFactory Factory = cfg.BuildSessionFactory(); ISession session = Factory.OpenSession(); IQuery query = session.CreateQuery("from Login"); IList<Login> list = query.List<Login>(); foreach (Login login in list) { MessageBox.Show(login.Name); } }
public class Nhelp { public IList<T> Query<T>() { Configuration cfg = new Configuration().Configure(); ISessionFactory Factory = cfg.BuildSessionFactory(); ISession session = Factory.OpenSession(); IQuery query = session.CreateQuery("from login"); IList<T> list = query.List<T>(); return list; } }
private void Form1_Load(object sender, EventArgs e) { Nhelp help = new Nhelp(); IList<Login> list = help.Query<Login>(); foreach (Login login in list) { MessageBox.Show(login.Name); } }
在 NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
在 NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
在 NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
在 demo.Nhelp.Query[T]() 位置 D:\Projects\WindowApp\demo\Nhelp.cs:行号 22
在 demo.Form1.Form1_Load(Object sender, EventArgs e) 位置 D:\Projects\WindowApp\PanasonicMES\PanasonicMES\Form1.cs:行号 39
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 PanasonicMES.Program.Main() 位置 D:\Projects\WindowApp\demo\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
实在不知道 错在何处 求解
[解决办法]
IQuery query = session.CreateQuery("from login");
区分大小写吗?
IQuery query = session.CreateQuery("from Login");
[解决办法]
晕菜,是大小写问题,hql里的实体类名是大小写敏感的。
Query<T> 每次调用都会执行这两句:
Configuration cfg = new Configuration().Configure();
ISessionFactory Factory = cfg.BuildSessionFactory();
这是没必要的,浪费资源。在应用程序启动的时候配置一次就可以了。
session 用完之后要关闭: session.Close,建议把 session 放在 using 语句里。