nHibernate 和并发检查
我想使用 UnitOfWork 模式使用 nHibernate 3 实现并发检查。
更准确地说:
- 打开新会话,
- 在会话中加载实体,
- 关闭会话,
- 给用户一些时间在加载的实体中编辑数据,
- 打开新会话,
- 更新数据
- 关闭会话。
我正在使用时间戳来版本实体。
这是我的映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="...."
namespace="...."
default-lazy="false">
<class name="Employee"
optimistic-lock="version"
dynamic-update="true">
<id name="Id">
<generator class="native" />
</id>
<version column="LastEditDate" generated="always" type="timestamp" />
<property name="Name" not-null="1" length="255" />
<property name="LastEditUser" not-null="1" length="255"/>
</class>
</hibernate-mapping>
我不知道如何在会话上下文中更新实体
var entity = <updated by user>
using (var session = GetNewSession())
{
//todo: load current version value / attach entity to context
session.SaveOrUpdate(entity);
//if concurency check fails, StaleObjectException (or similar) is expected to be thrown
}
在 SQL 中它应该像这样工作
UPDATE ENTITY SET LastEditDate = @P1, ... WHERE ID = @P2 AND LastEditDate = @P3
where:
@P1 - new LastEditDate
@P2 - entity ID
@P3 - previous LastEditDate
如果 ROWSMODIFIED = 1 那么更新成功,否则如果 = 0 那么 ConcurrencyException
使用 Linq2Sql 这非常简单:创建版本控制列,附加实体到新的会话上下文并尝试更新。
我怎样才能在 nHiberate 中做到这一点?支持吗?
I want to achieve concurrency check using nHibernate 3 using UnitOfWork pattern.
To be more precise:
- open new session session,
- load entity in a session,
- close session,
- give user some time to edit data in loaded entity,
- open new session,
- update data
- close session.
I'm using timestap to version entity.
Here is my mapping file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="...."
namespace="...."
default-lazy="false">
<class name="Employee"
optimistic-lock="version"
dynamic-update="true">
<id name="Id">
<generator class="native" />
</id>
<version column="LastEditDate" generated="always" type="timestamp" />
<property name="Name" not-null="1" length="255" />
<property name="LastEditUser" not-null="1" length="255"/>
</class>
</hibernate-mapping>
I have no idea how to update entity in session context
var entity = <updated by user>
using (var session = GetNewSession())
{
//todo: load current version value / attach entity to context
session.SaveOrUpdate(entity);
//if concurency check fails, StaleObjectException (or similar) is expected to be thrown
}
In SQL it should work like this
UPDATE ENTITY SET LastEditDate = @P1, ... WHERE ID = @P2 AND LastEditDate = @P3
where:
@P1 - new LastEditDate
@P2 - entity ID
@P3 - previous LastEditDate
If ROWSMODIFIED = 1 then update was successfull, else if = 0 then ConcurrencyException
Using Linq2Sql it was very simple: create versioning column, attach entity to new session context and try to update.
How can I do it in nHiberate? Is it supported?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该足够了。
should be enough.
我认为你应该使用
session.Lock(entity,LockMode.Update)
。I think you should use
session.Lock(entity,LockMode.Update)
.