Oracle Native ID generation with NHibernate

来源:互联网 发布:淘宝app怎么卖二手东西 编辑:程序博客网 时间:2024/06/02 02:03

Native ID generation with NHibernate

原文出处 : http://davybrion.com/blog/2007/07/native-id-generation-with-nhibernate/

 

 

iwas looking for a way to define the mapping of an ID field so it wouldwork on both Oracle and SQL Server… In case of Oracle, it would have touse a Sequence that i can define. On Sql Server, it should use anIdentity column. Luckily, NHibernate offers just that… the ‘native’ IDgenerator uses identity on sql server and a sequence on Oracle.

Unfortunately the ‘native’ generator is hardly documented in theNhibernate reference documentation so it wasn’t really clear to mewhich sequence would be used, or how i could define my own. All thedocumentation says is basically:

For cross-platform development, the native strategy willchoose from the identity, sequence and hilo strategies, dependent uponthe capabilities of the underlying database.

So how can you define which sequence it should use in case ofOracle? Very simple actually… turns out you just define the sequencename in the same way you would as if you set it to use the ’sequence’generator.

So here’s how you do it:

    <id name="Id" column="CustomerId" type="long" unsaved-value="-1"
access="field.camelcase-underscore">
<generator class="native" >
<param name="sequence">sq_customer</param>
</generator>
</id>

On Oracle, it will use the sq_customer sequence whereas on SQLServer the sequence parameter will be ignored and it will use theIdentity setting of the column that was defined as the ID (in thiscase, CustomerId’s Identity settings)

 

当访问的是MS SQLServer时,<param name="sequence">sq_customer</param>会被忽略,而使用MS SQLServer的Identity方式来生成ID;当访问的时Oracle数据库时,使用
<param name="sequence">sq_customer</param>中的squence sq_customer来生成Id.
原创粉丝点击