I am trying to call a named query using NHibernate and am receiving errors upon execution.
This only happens when connecting to Sybase server and works fine on Microsoft SQL Server.
We are using Sybase ASE 15 and NHibernate 3.3.1.4000.
We do not have access to a Sybase Profiler. My guess is that NHibernate is not translating the query properly when sending to Sybase.
Here is where the parameters are being passed:
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();
Here is the NHibernate mapping:
<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>
The error is received when executing query.UniqueResult().
This is the error:
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]
And the Inner Exception:
{"Must declare variable '#p0'.\n"}
It is obvious that the variables are not being declared by NHibernate.
Has anybody experienced this before that may know why this is happening?
Is this an NHibernate issue or a configuration/setup issue?
Any help is greatly appreciated.
Thanks,
Joe
UPDATE:
Here are the configuration settings. Note that the Microsoft SQL settings are commented out. The Microsoft SQL settings work when connecting to a Microsoft SQL database. The Sybase settings do not work when connecting to a Sybase SQL server.
<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>
Check if is by sending null on your first parameter:
Here
For deal with null or optional parameters.
FYI, it works with the ODBC driver, but not with the SybaseASE driver.
Related
Learning NHibernate by following this tutorial Your first NHibernate based application and I got to the point where you call
new SchemaExport(cfg).Execute(true, true, false);
in a test method to export the schema (create the Product table) for verifying NHibernate was set up correctly
[Test]
public void Can_generate_schema()
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Product).Assembly);
new SchemaExport(cfg).Execute(true, true, false);
}
The mapping
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyFirstNHibernateProject" namespace="MyFirstNHibernateProject.Domain">
<class name="Product">
<id name="Id">
<generator class="guid"></generator>
</id>
<property name="Name"></property>
<property name="Category"></property>
<property name="Discontinued"></property>
</class>
</hibernate-mapping>
The configuration
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
<property name="connection.connection_string">Data Source=MyFirstNHibernateProject.sdf</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
The test passed and I can even see the output sql that create table but the table is not being created in the database (sql server compact), I refreshed the db in server explorer and it is still empty.
I have checked these posts
NHibernate SchemaExport not creating table
NHibernate SchemaExport does not create tables when “script” is false
NHibernate does not create Tables
but none of them solve my problem.
any ideas?
I would suggest to use full path in the connection string:
// instead of this
<property name="connection.connection_string"
>Data Source=MyFirstNHibernateProject.sdf</property>
// we should use
<property name="connection.connection_string"
>Data Source=C:\MyFirstNHibernateProject\MyFirstNHibernateProject.sdf</property>
Where the "C:\MyFirstNHibernateProject\" should be the full path to the "MyFirstNHibernateProject.sdf"
Also, in case you are suing CE 4.0 I would suggest to use this dialect:
<property name="dialect">NHibernate.Dialect.MsSqlCe40Dialect</property>
Today I updated my project to NHibernate 3.3 and replaced the NHibernate and the Iesi.Collections References.
I kept NHibernate.ByteCode.Castle
I removed the line
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> from my application config.
Now I get the error NotSupportedException in the last line of the following code:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.AddAssembly(Assembly.GetExecutingAssembly());
sessionFactory = cfg.BuildSessionFactory();
I googled a lot but I don't have a clue what's wrong. And ideas? Thanks!
my config:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="connection.connection_string">Server=myserverip;Port=myport;database=my_test;User Id=my_user;Password=mypwd;CommandTimeout=1;</property>
<!--<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>-->
<property name="command_timeout">0</property>
</session-factory>
Just add this line to your xml config
<property name="hbm2ddl.keywords">none</property>
More details described here:
C# / Postgres / FluentNHibernate : configuring npgsql throws NotSupportedException
I am using Visual Studio 2005 with NHibernate-2.1.2.GA in Windows Server 2003.
I try to connect oracle with NHibernate in my ASP.net program.
To get the version of my oracle, i run this SQL : select * from v$version
The result show the oracle version is oracle9i.
So, I write NHibernate.cfg.xml like this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=MYORACLE)));
user id=team;password=team;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
</property>
<mapping assembly="NHibernateSample.Domain"/>
</session-factory>
</hibernate-configuration>
Now, when I run the test unit which try to connect oracle and excute a query sql :
private ISession _session;
private SessionManager _helper;
private NHibernateSample _sample;
public void TestFixtureSetup()
{
_helper = new SessionManager();
}
public void Setup()
{
_session = _helper.GetSession();
_sample = new NHibernateSample(_session);
}
[NUnit.Framework.Test]
public void GetCustomerById1Test()
{
TestFixtureSetup();
Setup();
NHibernateSample _sample = new NHibernateSample(_session);
Assert.AreEqual(1, _sample.GetCustomerById(1).Id);
}
it reports errors like the following:
Test 'NHibernateSample.Data.Test.NHibernateSampleFixture.GetCustomerById1Test' failed: NHibernate.MappingException : Could not compile the mapping document: NHibernateSample.Domain.Mappings.Customer.hbm.xml
----> NHibernate.HibernateException : Could not instantiate dialect class NHibernate.Dialect.Oracle9iDialect
----> System.TypeLoadException : Could not load type org.NHibernate.Dialect.Oracle9iDialect. Possible cause: no assembly name specified.
I know that this is beginner's issue but I am just a newbie! I appreciate your ideas...
Try again removing org. from dialect
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
Check NHibernate SQL Dialects from official documentation for more details.
Thanks God,Finally I fix this problem.
Thers is a message in result which i forget to paste above,it's like this:
未能从程序集“mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.DateTimeOffset”
It's caused because my .Net2.0 version is old.
Then i install .Net2.0 SP1 and the probelm is fixed!
Thanks Claudio Redi all the same!
I'm using NHibernate with a web application and Oracle 10g XE, and I'm currently getting the following error:
ORA-06413: Connection not open.
My guess is it's a problem with my connection string. Here's my Hibernate configuration in Web.config:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="MyProject.MyAssembly">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)));User ID=myid;Password=mypassword</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="Domain" />
</session-factory>
</hibernate-configuration>
I've also tried using the following connection string:
<property name="connection.connection_string">
User ID=myid;Password=mypassword;Data Source=localhost
</property>
That resulted in the following error:
ORA-12154: TNS:could not resolve the connect identifier specified.
Could anyone provide any insight into what I might be doing wrong here?
Edit
I created a console application project inside the same solution. I created a hibernate.cfg.xml file with the same settings and added it to that project. I can persist objects with no problems.
I've switched my database from Oracle 10g to MySQL and it works fine in the web application. Why does Oracle 10g only work in the console application and not in the web application?
Apparently Oracle 10g is a bit finicky on 64-bit systems. The best option seemed to be switching to MySQL.
I faced this error today and found interesting thing.
I tried to use XE client oracle.dataaccecc.dll as reference and before the statement
sessionFactory = new NHibernate.Cfg.Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NHibernate\\Oracle.cfg.xml")).BuildSessionFactory();
i entered these lines:
OracleConnection conn = new OracleConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connString"].ConnectionString);
conn.Open();
conn.Close();
And everything started to work.
Can someone tell me whats wrong with the nhibernate config below? Using the latest ODP.NET version.
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">DATA SOURCE=Oracle2;PERSIST SECURITY INFO=True;USER ID=***;PASSWORD=*****</property>
<property name="show_sql">true</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
I get the following error
Error 1 Test 'ODPNETNH.Tests.GenerateSchema_Fixture.Can_generate_schema' failed: NHibernate.MappingException : Could not compile the mapping document: ODPNETNH.Mappings.Vendor.hbm.xml
----> NHibernate.HibernateException : Could not instantiate dialect class NHibernate.Dialect.Oracle9Dialect
----> System.TypeLoadException : Could not load type NHibernate.Dialect.Oracle9Dialect. Possible cause: no assembly name specified.
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
I had quite a few problems working with NHibernate and Oracle (ODP.NET).
At the end the only thing which worked for me was to reference Oracle.DataAccess.dll ver. 2.111, specifying Copy Local = true for the DLL.
In my Nhibernate config I've used Oracle10gDialect.
Last but not least I've used this new connection-string style:
<property name="connection.connection_string">
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1522)))(CONNECT_DATA=(SERVICE_NAME=DIAP60FF)));User ID=myusername;Password=mypassword;</property>
I think I figured it out. Had to call Configure("hibernate.cfg.xml");