nhibernate-条目从未保存到nhibernate中的db

oo7oh9g9  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(267)

我试图创建一个具有4个属性的对象;1个id和3个对其他表的引用。
我知道我不应该将nhibernate“session.save()”与sqlinsert语句进行比较,但我仍然希望它在请求/线程完成后将其保存到数据库中。我的代码是这样的:

var session = Home.Instance.GetSession();
session.Begin();
var profile = session.Get<ProfilePO>(profileId);
var queue = session.Get<QueuePO>(queueId);
var telephone = session.Get<TelephonePO>(telephoneId);
var newObject = new UserProfileControlledQueueMembersPO
            {
                Profile = profile,
                Queue = queue,
                Telephone = telephone
            };
session.Save(newObject);
var idOfNewObj = newObject.Id; //This gets the correct value from the autoincrementing ID in the mysql database.
var newObjFromCacheOrDb = session.QueryOver<UserProfileControlledQueueMembersPO>().Where(x => x.Id == idOfNewObj).List().FirstOrDefault();
//newObjFromCacheOrDb actually return the correct values of the created object

“session”来自 Package 类:

private int _transactionCount;
private ITransaction _transaction;
private ISession Session { get; set; }
public void Begin()
{
  if (_transactionCount <= 0)
  {
    _transaction = Session.BeginTransaction();
    _transactionCount = 0;
  }
  _transactionCount++;
}
public object Save<T>(T ob) where T : TEntityType
{
  if (_transactionCount <= 0)
  throw new Exception("Save is not allowed without an active transaction!");

  return Session.Save(ob);
}

在nhibernate的日志中我发现:
debug nhibernate.sql(null)-插入配置文件队列电话(profileid,queueid,telephoneid)值(?p0,?p1,?p2);?p0=7283[类型:int32(0:0:0)],?p1=27434[类型:int32(0:0:0)],?p2=26749[类型:int32(0:0:0)]
debug nhibernate.sql(null)-选择最后一个\u insert \u id()
debug nhibernate.sql(null)-从profile\u queue\u telephone this中选择this\uID为id1\u 45\u 0\u,this\uProfileID为profileid2\u 45\u 0\u,this\uID为queueid3\u 45\u 0\u,this\uID为telephone4\u 45\u 0\u;?p0=74[型号:int32(0:0:0)]
我不明白为什么不在数据库上执行这个操作,因为nhibernate清楚地将它存储在某种缓存中,因为我能够检索数据。如果是因为回滚,我假设日志中会声明,mysql服务器上发生了回滚。
所以我的问题是我在这里遗漏了什么,我该怎么做才能把对象插入数据库?
编辑:
值得注意的是,我对nhibernate还很陌生。我在做一个4年多的项目,是和nhibernate一起写的。

weylhg0b

weylhg0b1#

你的 Package 方法 session.Begin() 启动事务,但从不调用相应的 Commit 将更改永久保存到数据库。否则,更改将被回滚,而数据库实际上不会受到影响。您的 Package 器方法之一应该调用 _transaction.Commit ,找到该方法并调用它。

相关问题