我正在尝试使用 NHibernate 调用命名查询,但在执行时收到错误。 这仅在连接到 Sybase 服务器时发生,并且在 Microsoft SQL Server 上运行良好。
我们使用的是 Sybase ASE 15 和 NHibernate 3.3.1.4000。
我们无权访问 Sybase Profiler。我的猜测是 NHibernate 在发送到 Sybase 时没有正确翻译查询。
这里是传递参数的地方:
NHibernate.IQuery query;
query = NHibernateDataContext.GetNamedQuery("UPDATE_INVESTOR_TARGET");
query.SetParameter("pTargetId", target.Id.TargetId < 1 ? null :(int?) target.Id.TargetId, NHibernate.NHibernateUtil.Int32);
query.SetParameter("pInvestorId", target.InvestorId, NHibernate.NHibernateUtil.String);
query.SetParameter("pReleaseStatus", new TargetReleaseStatusEnumerationStringType().GetValue(target.ReleaseStatus), NHibernate.NHibernateUtil.String);
query.SetParameter("pEventId", target.Id.EventId, NHibernate.NHibernateUtil.Int32);
query.SetParameter("pAccountName", target.InvestorName.Value, NHibernate.NHibernateUtil.String);
query.SetParameter("pLastChangedBy", lastChangedBy, NHibernate.NHibernateUtil.String);
target.Id.TargetId = (int)query.UniqueResult();
trans.CommitChanges();
这是 NHibernate 映射:
<sql-query name="UPDATE_INVESTOR_TARGET" cacheable="false" >
<return-scalar column="Returned Value" type="int" />
EXECUTE sUDI_COMPS
@pSelectReturnYN = 'Y',
@pAccountNo = :pInvestorId,
@pReleaseStatus = :pReleaseStatus,
@pAccountName = :pAccountName,
@pLastChangedBy = :pLastChangedBy,
@pDealNo = :pEventId,
@pCompsNo = :pTargetId
</sql-query>
执行query.UniqueResult()时收到错误。
这是错误:
could not execute query [ EXECUTE sUDI_COMPS @pSelectReturnYN = 'Y', @pAccountNo = @p0, @pReleaseStatus = @p1, @pAccountName = @p2, @pLastChangedBy = @p3, @pDealNo = @p4, @pCompsNo = @p5 ]
Name:pTargetId - Value:1 Name:pInvestorId - Value:1 Name:pReleaseStatus - Value:R Name:pEventId - Value:1 Name:pAccountName - Value:45cda18d-f4da-4342-909b-491918fb4032 Name:pLastChangedBy - Value:jmacri [SQL: EXECUTE sUDI_COMPS @pSelectReturnYN = 'Y', @pAccountNo = @p0, @pReleaseStatus = @p1, @pAccountName = @p2, @pLastChangedBy = @p3, @pDealNo = @p4, @pCompsNo = @p5]
以及内部异常:
{"Must declare variable '@p0'.\n"}
很明显,变量不是由 NHibernate 声明的。
有没有人之前经历过这种情况,可能知道为什么会这样?
这是 NHibernate 问题还是配置/设置问题?
非常感谢任何帮助。
谢谢, 乔
更新:
这里是配置设置。请注意,Microsoft SQL 设置已被注释掉。 Microsoft SQL 设置在连接到 Microsoft SQL 数据库时起作用。连接到 Sybase SQL 服务器时,Sybase 设置不起作用。
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="ICM">
<!-- MS SQL 2008 Server -->
<!--<property name="connection.driver_class">
Dealogic.DataAccess.Driver.DealogicSqlClientDriver,Dealogic.DataAccess
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2008Dialect
</property>
<property name="connection.connection_string_name">default</property>-->
<!-- Sybase ASE 15 Server -->
<property name="connection.driver_class">
Dealogic.DataAccess.Driver.DealogicSybaseAseClientDriver,Dealogic.DataAccess
</property>
<property name="dialect">
Dealogic.DataAccess.Dialect.ExtendedSybaseAse15Dialect,Dealogic.DataAccess
</property>
<property name="connection.connection_string_name">default</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="generate_statistics">false</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
最佳答案
关于c# - 在 NHibernate 中调用 Named Query 并将参数传递给 Sybase Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821558/