NHibernate - 条目永远不会保存到 Nhibernate 中的数据库
NHibernate - Entry never getting saved to db in Nhibernate
我正在尝试创建一个具有 4 个属性的对象; 1x ID 和 3x 对其他表的引用。
我知道我不应该将 NHibernate“session.Save()”与 sql Insert 语句进行比较,但我仍然希望它在 request/thread完成。
这是我的代码的样子:
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
“会话”来自包装器 class:
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) - INSERT INTO Profile_Queue_Telephone (ProfileId, QueueId, TelephoneId) VALUES (?p0, ?p1, ?p2);?p0 = 7283 [Type: Int32 (0:0:0)], ?p1 = 27434 [Type: Int32 (0:0:0)], ?p2 = 26749 [Type: Int32 (0:0:0)]
DEBUG NHibernate.SQL (null) - SELECT LAST_INSERT_ID()
DEBUG NHibernate.SQL (null) - SELECT this_.Id as id1_45_0_, this_.ProfileId as profileid2_45_0_, this_.QueueId as queueid3_45_0_, this_.TelephoneId as telephone4_45_0_ FROM Profile_Queue_Telephone this_ WHERE this_.Id = ?p0;?p0 = 74 [Type: Int32 (0:0:0)]
我很困惑为什么这不在数据库上执行,因为 NHibernate 显然将它存储在某种缓存中,因为我能够检索数据。
如果是因为回滚,我假设日志中已经说明了回滚发生在 MySql 服务器上。
所以我的问题是我在这里遗漏了什么,我该怎么做才能将对象插入到数据库中?
编辑:
可能值得注意的是,我是 NHibernate 的新手。我正在做一个 4 年多的项目,用 NHibernate 编写。
您的包装方法 session.Begin()
启动了一个事务,但您从未调用相应的 Commit
来永久保存对数据库的更改。否则,更改将被回滚并且您的数据库实际上不会受到影响。您的包装器方法之一应该调用 _transaction.Commit
,找到该方法并调用它。
我正在尝试创建一个具有 4 个属性的对象; 1x ID 和 3x 对其他表的引用。
我知道我不应该将 NHibernate“session.Save()”与 sql Insert 语句进行比较,但我仍然希望它在 request/thread完成。 这是我的代码的样子:
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
“会话”来自包装器 class:
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) - INSERT INTO Profile_Queue_Telephone (ProfileId, QueueId, TelephoneId) VALUES (?p0, ?p1, ?p2);?p0 = 7283 [Type: Int32 (0:0:0)], ?p1 = 27434 [Type: Int32 (0:0:0)], ?p2 = 26749 [Type: Int32 (0:0:0)]
DEBUG NHibernate.SQL (null) - SELECT LAST_INSERT_ID()
DEBUG NHibernate.SQL (null) - SELECT this_.Id as id1_45_0_, this_.ProfileId as profileid2_45_0_, this_.QueueId as queueid3_45_0_, this_.TelephoneId as telephone4_45_0_ FROM Profile_Queue_Telephone this_ WHERE this_.Id = ?p0;?p0 = 74 [Type: Int32 (0:0:0)]
我很困惑为什么这不在数据库上执行,因为 NHibernate 显然将它存储在某种缓存中,因为我能够检索数据。 如果是因为回滚,我假设日志中已经说明了回滚发生在 MySql 服务器上。
所以我的问题是我在这里遗漏了什么,我该怎么做才能将对象插入到数据库中?
编辑:
可能值得注意的是,我是 NHibernate 的新手。我正在做一个 4 年多的项目,用 NHibernate 编写。
您的包装方法 session.Begin()
启动了一个事务,但您从未调用相应的 Commit
来永久保存对数据库的更改。否则,更改将被回滚并且您的数据库实际上不会受到影响。您的包装器方法之一应该调用 _transaction.Commit
,找到该方法并调用它。