NHibernate ISession.Save() - 为什么这会立即保留我的实体?
我正在使用 NHibernate 创建大量实体,将它们附加到我的 ISession,然后使用事务将更改提交到数据库。代码示例如下:
ISession _context = SessionProvider.OpenSession();
//Create new entities
for(int i=0; i<100; i++)
{
MyEntity entity = new MyEntity(i);
//Attach new entity to the context
_context.Save(entity);
}
//Persist all changes to the database
using(var tx = _context.BeginTransaction())
{
//Flush the session
tx.Commit();
}
我的印象是 _context.Save() 行只是让 ISession 知道新实体,但在我通过 tx.Commit() 行刷新会话之前,不会将任何更改保留到数据库中。
但我观察到,每次我调用 _context.Save() 时,数据库都会获取一个新实体。结果,我对数据库进行了太多的单独调用。
有谁知道为什么 ISession.Save() 会自动保存更改?我是否误解了 NHibernate 的行为方式?谢谢。
***编辑 - 只是为了澄清(根据两个建议的答案) - 我的问题是,一旦我调用 _context.Save() ,数据库就会更新。我不希望这种情况发生。我希望在调用 tx.Commit() 之前不会将任何内容插入到数据库中。不幸的是,到目前为止,两个建议的答案都没有帮助解决这个问题。
可以找到有关身份生成器的一些有用信息 这里
I am creating a large number of entities with NHibernate, attaching them to my ISession, and then using a transaction to commit my changes to the database. Code sample is below:
ISession _context = SessionProvider.OpenSession();
//Create new entities
for(int i=0; i<100; i++)
{
MyEntity entity = new MyEntity(i);
//Attach new entity to the context
_context.Save(entity);
}
//Persist all changes to the database
using(var tx = _context.BeginTransaction())
{
//Flush the session
tx.Commit();
}
I was under the impression that the line _context.Save() simply makes the ISession aware of the new entity, but that no changes are persisted to the database until I Flush the session via the line tx.Commit().
What I've observed though, is that the database gets a new entity every time I call _context.Save(). I end up with too many individual calls to the database as a result.
Does anyone know why ISession.Save() is automatically persisting changes? Have I misunderstood something about how NHibernate behaves? Thanks.
***EDIT - Just to clarify (in light of the two suggested answers) - my problem here is that the database IS getting updated as soon as I call _context.Save(). I don't expect this to happen. I expect nothing to be inserted into the database until I call tx.Commit(). Neither of the two suggested answers so far helps with this unfortunately.
Some good information on identity generators can be found here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试:
Try:
您使用哪种身份生成器?如果您使用 MSSQL/MySQL 的
Identity
或 Oracle 的sequence
等插入后生成器来生成 Id 字段的值,那就是您的问题。来自NHibernate POID 生成器揭晓:
Which identity generator are you using? If you are using post-insert generators like MSSQL/MySQL's
Identity
or Oracle'ssequence
to generate the value of your Id fields, that is your problem.From NHibernate POID Generators Revealed:
您可以在配置中设置批量大小:
或者您可以在代码中设置。并确保您在事务范围内进行保存。
You can set your batch size in your configuration:
Or you can set it in code. And make sure you do your saves within a transaction scope.
尝试将 FlushMode 设置为 Commit:
同行设置批量大小的建议也很好。
我的理解是,当使用数据库标识列时,NHibernate 将推迟插入,直到刷新会话,除非它需要执行插入以检索外键或确保查询返回预期结果。
Try setting the FlushMode to Commit:
peer's suggestion to set the batch size is good also.
My understanding is that when using database identity columns, NHibernate will defer inserts until the session is flushed unless it needs to perform the insert in order to retrieve a foreign key or ensure that a query returns the expected results.
好吧,
Well
怎么样:
What about :