Nhibernate MultiQuery
構造多個查詢。效果不錯。注意Nhibernate CreateMultiQuery()這個方法
public void MultiQueryTest(int var) {
ISession session = null; session = Class1.GetFactory().OpenSession(); IList result = session.CreateMultiQuery().Add("from Archives where id>:id") .Add("select count(*) from Archives a where id>:id").SetInt32("id", var).List(); //獲得第一個查詢結果 IList archives = (IList)result[0]; long count = (long)((IList)result[1])[0]; //當然也可以session.CreateQuery 構造查詢 //IList result = session.CreateMultiQuery() // .Add(session.CreateQuery("from Archives where id>:id").SetFirstResult(5).SetMaxResults(10)) // .Add(session.CreateQuery("select count(*) from Archives a where id>:id")) // .SetCacheable(true).SetInt32("id", var).List(); // IList archives = (IList)result[0]; //long count = (long)((IList)result[1])[0]; }
//helper
public class Class1
{
private static ISessionFactory factory;
private static string _mappingAssembly;
static Class1()
{
_mappingAssembly = ConfigurationManager.AppSettings["EntityAssemblyName"];
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
foreach (string assembly in _mappingAssembly.Split(','))
{
try
{
cfg.AddAssembly(assembly);
}
catch (MappingException me)
{
throw new MappingException("Assembley Could Not Be Loaded: " + assembly, me);
}
}
factory = cfg.BuildSessionFactory();
}
public static ISessionFactory GetFactory()
{
return factory;
}
}
更多精彩關注 http://www.ayende.com/
posted on 2007-05-12 21:18 record java and net 閱讀(841) 評論(0) 編輯 收藏