[英]NHibernate and Integer Columns as Version
I'm trying to create a DB Table, using an NHibernate *hbm.xml mapping file, that will have a Versioning Column for concurrency check. 我正在尝试使用NHibernate * hbm.xml映射文件创建一个DB表,它将具有用于并发检查的版本控制列。 The Versioning column should be a nullable Integer.
版本控制列应该是可以为空的整数。
Although the Database is created just fine, using the mapping file as reference, the following happen: * The first record is inserted with a NULL value as the Version * The update of the previously inserted records fails with a "Stale Data" exception 虽然数据库创建得很好,但使用映射文件作为参考,会发生以下情况:*第一个记录插入时带有NULL值作为版本*先前插入的记录的更新失败,并显示“过时数据”异常
In other words, no matter what I do, the Version column is always NULL. 换句话说,无论我做什么,Version列始终为NULL。 I'm somewhat new to the Concurrency Control using NHibernate, so I don't quite understand what I'm doing wrong..
我对使用NHibernate的并发控制有点新,所以我不太明白我做错了什么..
If I use a Timestamp as a Version, everything works just fine. 如果我使用Timestamp作为版本,一切正常。 However, my requirement is to use an Integer.. Hence my problem.
但是,我的要求是使用整数..因此我的问题。
This is my Mapping File: 这是我的映射文件:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" auto-import="false" assembly="New1.Backend" namespace="New1.BO">
<class name="Natrio" table="`Natrios`" schema="`dbo`">
<cache usage="read-write" />
<id name="Id" column="`Id`" type="System.Int32">
<generator class="NHibernate.Id.Enhanced.TableGenerator">
<param name="increment_size">200</param>
<param name="segment_value">Natrios</param>
<param name="optimizer">pooled-lo</param>
</generator>
</id>
<version name="Version" column="`Version`" type="System.Nullable`1[[System.Int32, mscorlib]], mscorlib" generated="always" unsaved-value="0">
<column name="`Version`" not-null="false" sql-type="int" />
</version>
<property name="Attribute" column="`Attribute`" type="String" not-null="false" length="100" />
</class>
</hibernate-mapping>
Any thoughts and/or suggestions would be greatly appreciated! 任何想法和/或建议将不胜感激!
Why do you need nullable version column? 为什么需要可空版本列? In any case I believe the issue is caused by
unsaved-value="0"
in your mapping. 在任何情况下,我认为问题是由映射中的
unsaved-value="0"
引起的。 As default value for nullable column is null
- NHibernate thinks that value is already generated and so it's never assigned. 由于可空列的默认值为
null
- NHibernate认为该值已经生成,因此从未分配。 You need to set it to null - unsaved-value="null
" to make it work with nullable columns. 您需要将其设置为null -
unsaved-value="null
”以使其与可空列一起使用。 And unsaved-value="0"
makes sense for not-nullable types. 而
unsaved-value="0"
对于不可空类型是有意义的。 But better omit this attribute completely and let NHibernate to s 但最好完全省略这个属性,让NHibernate使用
Another issue with generated
attribute. generated
属性的另一个问题。 It's about DB generation - so always
means that this value is generated automatically by DB. 它是关于数据库生成的 - 所以
always
意味着这个值是由DB自动生成的。 You should remove it or specify it as generated="never"
. 您应该删除它或将其指定为
generated="never"
。
I believe the following mapping should work for you: 我相信以下映射应该适合您:
<version name="Version">
<column name="`Version`" not-null="false" sql-type="int" />
</version>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.