简单问题我可以这样做:
(我不知道另一种方法,因为NHibernate不支持嵌套的事务)
public class GetNextSequence : IIdentifierGenerator
{
public object Generate(NHibernate.Engine.ISessionImplementor session, object obj)
{
using (var nSession = SessionFactoryWrapper.SessionFactory.OpenSession())
{
using( var tran = nSession.BeginTransaction(System.Data.IsolationLevel.Serializable))
{
var update = nSession.CreateSQLQuery("update counter set last_one = last_one + 1 where item like :item");
update.SetParameter("item", "account_contact.account_contact_id");
update.ExecuteUpdate();
var query = nSession.CreateSQLQuery("SELECT last_one FROM counter WHERE item LIKE :item");
query.SetParameter("item", "account_contact.account_contact_id");
var lastOne = query.UniqueResult();
tran.Commit();
return lastOne;
}
}
}
}
或者这是我不该做的事情,如果是这样,为什么不呢?
答案 0 :(得分:1)
你可以试试这样的事情
public class GetNextSequence : IIdentifierGenerator, IConfigurable
{
private string _item;
public object Generate(NHibernate.Engine.ISessionImplementor session, object obj)
{
using (var nSession = SessionFactoryWrapper.SessionFactory.OpenSession())
{
using( var tran = nSession.BeginTransaction(System.Data.IsolationLevel.Serializable))
{
var update = nSession.CreateSQLQuery("update counter set last_one = last_one + 1 where item like :item");
update.SetParameter("item", _item);
update.ExecuteUpdate();
var query = nSession.CreateSQLQuery("SELECT last_one FROM counter WHERE item LIKE :item");
query.SetParameter("item", _item);
var lastOne = query.UniqueResult();
tran.Commit();
return lastOne;
}
}
}
public void Configure(NHibernate.Type.IType type, IDictionary<string, string> parms, NHibernate.Dialect.Dialect dialect)
{
parms.TryGetValue("item", out _item);
}
}