简体   繁体   English

nHibernate:保存之前确定属性值

[英]nHibernate: determine property value before save

we are currently evaluating whether nHibernate supports the requirements for our project. 我们目前正在评估nHibernate是否支持我们项目的要求。 We share the database with another application so that we are not completely free as regards changes to the schema. 我们与另一个应用程序共享数据库,因此就架构更改而言,我们并非完全自由。 Some columns are filled with unique and consecutive numbers (eg for invoices). 一些列填充有唯一且连续的数字(例如,发票)。 The next number is determined by a stored procedure that also implements a locking algorithm so that the numbers are guaranteed to be consecutive. 下一个数字由存储过程确定,该存储过程也实现了锁定算法,从而保证了这些数字是连续的。 On the one hand we could define a trigger on the respective tables that sets the value for the column when an empty or special value is provided. 一方面,我们可以在相应的表上定义一个触发器,以在提供空值​​或特殊值时设置该列的值。 This would require changing the existing database definition - though it might be the most reliable way to implement this. 这可能需要更改现有的数据库定义-尽管这可能是实现此定义的最可靠方法。 In order to avoid the change of the database definition we are trying to solve this in the nHibernate ORM. 为了避免更改数据库定义,我们试图在nHibernate ORM中解决此问题。 We've first tried to implement a user type that calls the stored procedure in NullSafeSet if an empty value is provided. 我们首先尝试实现一种用户类型,如果提供了空值,该用户类型将在NullSafeSet中调用存储过程。 Unfortunately, the connection and transaction of the provided command are not set yet when NullSafeSet is called. 不幸的是,调用NullSafeSet时,尚未设置所提供命令的连接和事务。

How can we solve this with nHibernate? 我们如何使用nHibernate解决这个问题?

Thanks in advance, 提前致谢,

Markus 马库斯

If you decide to go with trigger route, then you'll need to add generated attribute to your property mapping. 如果决定采用触发路线,则需要将生成的属性添加到属性映射中。

Generated properties are properties which have their values generated by the database. 生成的属性是其值由数据库生成的属性。 Typically, NHibernate applications needed to Refresh objects which contain any properties for which the database was generating values. 通常,NHibernate应用程序需要刷新对象,这些对象包含数据库为其生成值的任何属性。 Marking properties as generated, however, lets the application delegate this responsibility to NHibernate. 但是,将属性标记为已生成,可以使应用程序将此职责委托给NHibernate。 Essentially, whenever NHibernate issues an SQL INSERT or UPDATE for an entity which has defined generated properties, it immediately issues a select afterwards to retrieve the generated values. 本质上,每当NHibernate对已定义了生成属性的实体发出SQL INSERT或UPDATE时,它随后都会立即发出选择以检索生成的值。

Aside from that, I'm not quite sure how would you call stored procedure from NHibernate issued INSERT, without adding a trigger or default constraint on column. 除此之外,我不确定在不对列添加触发器或默认约束的情况下,如何从NHibernate发行的INSERT调用存储过程。


Edit 编辑

Looks like NHibernate has a notion of class persisters, through the interface IEntityPersister . 看起来NHibernate通过IEntityPersister接口具有持久性的概念。 Maybe you could hack something out from that. 也许您可以从中破解一些东西。

The persister attribute lets you customize the persistence strategy used for the class. 使用persister属性,您可以自定义用于该类的持久性策略。 You may, for example, specify your own subclass of NHibernate.Persister.EntityPersister or you might even provide a completely new implementation of the interface NHibernate.Persister.IClassPersister that implements persistence via, for example, stored procedure calls, serialization to flat files or LDAP. 例如,您可以指定自己的NHibernate.Persister.EntityPersister子类,或者甚至可以提供NHibernate.Persister.IClassPersister接口的全新实现,该接口通过例如存储过程调用,序列化到平面文件或LDAP。 See NHibernate.DomainModel.CustomPersister for a simple example (of "persistence" to a Hashtable). 请参阅NHibernate.DomainModel.CustomPersister以获取一个简单示例(“持久化”到Hashtable)。

You could start from NHibernate's source . 您可以从NHibernate的来源开始。

If you have the ability to add triggers to database, that would probably be the best, straightforward way, without investing too much time to fight with NHibernate's internals. 如果您有能力向数据库添加触发器,那可能是最好的,直接的方法,而无需花费太多时间与NHibernate的内部进行斗争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM