如何通过 NHibernate 从 oracle 序列中获取 NextVal

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4629046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 22:22:32  来源:igfitidea点击:

How do I get the NextVal from an oracle Sequence thru NHibernate

oraclenhibernatesequencenextval

提问by trainer

I am working on c# .net 4.0 and using NHibernate to talk with an Oracle DB. You would think something as simple as this is already addressed somewhere but sadly its not. I need the NextVal from an Oracle sequence. I do not need to insert it a database as part of an Id or Primary key. I just need to use the next val on the c# side.

我正在使用 c# .net 4.0 并使用 NHibernate 与 Oracle DB 对话。你会认为像这样简单的事情已经在某处解决了,但遗憾的是它没有。我需要 Oracle 序列中的 NextVal。我不需要将它插入数据库作为 Id 或主键的一部分。我只需要在 c# 端使用下一个 val。

Can somebody help me out with xml mapping and C# file(or a link) to achieve this.

有人可以帮助我使用 xml 映射和 C# 文件(或链接)来实现这一点。

Thanks.

谢谢。

Something like

就像是

int NextValueOfSequence = GetNextValueofSequence();

public int GetNextValueOfSequence()
{

// Access NHibernate to return the next value of the sequence.

}

回答by Petr Kozelek

Mapping:

映射:

  <sql-query name="GetSequence" read-only="true">
    <return-scalar type="Int64"/>
    <![CDATA[
    SELECT SeqName.NEXTVAL from DUAL;
    ]]>
  </sql-query>

Code:

代码:

Int64 nextValue = session.GetNamedQuery("GetSequence").UniqueResult<System.Int64>();

回答by trainer

This also does the trick.

这也能解决问题。

 <your session variable>.CreateSQLQuery("select <your sequence>.NEXTVAL from dual").UniqueResult<Int64>();

回答by Rox

With NH4 I use this DB agnostic ISession extension method (obviously DB must support sequences)

在 NH4 中,我使用这种与 DB 无关的 ISession 扩展方法(显然 DB 必须支持序列)

public static T GetSequenceNextValue<T>(this ISession session, string sequenceName) where T : struct
{
    var dialect = session.GetSessionImplementation().Factory.Dialect;
    var sqlQuery = dialect.GetSequenceNextValString(sequenceName);
    return session.CreateSQLQuery(sqlQuery).UniqueResult<T>();
}

回答by Gopal

There is a small correction in the mapping given by @Petr Kozelek

@Petr Kozelek 给出的映射中有一个小的更正

<sql-query name="GetSequence" read-only="true">
    <return-scalar column="NextNo" type="Int64"/>
    <![CDATA[
        SELECT SeqName.NEXTVAL as NextNo from DUAL
    ]]>
</sql-query>