[英]NHibernate - Entry never getting saved to db in Nhibernate
I'm trying to create a object with 4 properties; 我正在尝试创建一个具有4个属性的对象; 1x ID and 3x references to other tables. 1x ID和3x对其他表的引用。
I get that I shouldn't compare a NHibernate "session.Save()" with a sql Insert statement, but I would still expect it to save it in the database, after the request/thread is done. 我知道我不应该将NHibernate的“ session.Save()”与sql Insert语句进行比较,但是在请求/线程完成之后,我仍然希望它将其保存在数据库中。 This is what my code looks like: 这是我的代码如下所示:
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" is from a wrapper 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);
}
In the log for NHibernate I have found this: 在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(空)-插入到Profile_Queue_Telephone(ProfileId,QueueId,TelephoneId)值(?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() 调试NHibernate.SQL(空)-选择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)] DEBUG NHibernate.SQL(null)-选择this_.Id作为id1_45_0_,this_.ProfileId作为profileid2_45_0_,this_.QueueId作为queueid3_45_0_,this_.TelephoneId作为phone4_45_0_ FROM Profile_Queue_Telephone this_ WHERE this_.Id =?p0;?p0 = 74 Int32(0:0:0)]
I'm puzzled as to why this is not executed on the database, as NHibernate clearly stores it in some kind of cache, since I'm able to retrieve the data. 我很困惑为什么不在数据库上执行此操作,因为NHibernate清楚地将其存储在某种缓存中,因为我能够检索数据。 And if it was because of a rollback, I'm assuming that would have been stated in the log, that a rollback happened on the MySql server. 如果是由于回滚,我假设日志中已经说明了这一点,那就是在MySql服务器上发生了回滚。
So my question is what I'm missing here, what do I do to get the object inserted into the database? 所以我的问题是我在这里缺少什么,如何将对象插入数据库?
Edit: 编辑:
It might be worth noting that I'm very new to NHibernate. 值得一提的是,我对NHibernate还是很陌生。 I'm working on a 4+ year old project, written with NHibernate. 我正在用NHibernate编写一个4岁以上的项目。
Your wrapper method session.Begin()
starts a transaction but you never call the corresponding Commit
to permanently save the changes to the database. 包装器方法session.Begin()
启动事务,但是您从不调用相应的Commit
来将更改永久保存到数据库。 Without that, the changes will be rolled back and your database effectively untouched. 否则,更改将被回滚,并且数据库将保持不变。 One of your wrapper methods should be calling _transaction.Commit
, find that method and call it. 包装器方法之一应该是调用_transaction.Commit
,找到该方法并调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.