We're building a project using NHibernate and Castle with the Validators project. I'm trying to upgrade it to the latest supported version between all of those. I've gotten the application working without errors, but I'm getting the exception below in a few of my unit tests. These are tests that don't actually touch the database in any way, but test functionality around the mapped entities.
NHibernate.Bytecode.ProxyFactoryFactoryNotConfiguredException:
The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory
configuration section with one of the available NHibernate.ByteCode providers.
Example:
<property name='proxyfactory.factory_class'>
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
</property>
Example:
<property name='proxyfactory.factory_class'>
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
</property>
[Continues down stack trace...]
Below is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="Linx2">
<property
name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="dialect">Linx2.Common.Framework.PostgreSQL83Dialect,
Linx2.Common.Framework</property>
<property name="connection.connection_string">[Hidden so I don't get fired.]</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</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>
<property name="connection.release_mode">after_transaction</property>
<mapping assembly="NHibernate.Collection.Observable" />
</session-factory>
</hibernate-configuration>
I have the config mapping there, and it works in the application. I'm also including the NHibernate.ByteCode dll. However, in these tests it is ignored. I've tried manually starting the configuration in individual test and even stopped and confirmed mid-test that the configuration has the item. However, the exception is thrown in the code below on the IsInitialized call.
if (NHibernateUtil.IsInitialized(ChildAssociations))
{
ChildAssociations.ForEach(x => isValid = isValid && x.Element.IsValid(validatedObjects));
}
This worked previously with no problems in NHibernate build for 2.2. Any help would be greatly appreciated. I've been beating my head on it for the last 4 hours.
Apparently NHibernateUtil needs not only to have the configuration initialized, but needs the session factory to be built. I was able to get it to work by manually running the config and building the session factory in the tests. It wasn't a problem in the app because the session factory had been built before hand.
var cfg = new NHibernate.Cfg.Configuration().Configure();
var sessionFactory = cfg.BuildSessionFactory();
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!
The project structure is:
Now I am trying to create a bean NHibernateSessionFactory as:
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingResources">
<list>
<value>EMSApplication.Domain/EMSApplication.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<entry key="proxyfactory.factory_class" value="NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate"/>
<entry key="show_sql" value="true"/>
<entry key="hbm2ddl.auto" value="update"/>
<entry key="cache.use_query_cache" value="true"/>
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
But I am getting the error message:
Error creating context 'spring.root': file [C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\EMSApplication.Domain\EMSApplication.hbm.xml] cannot be resolved to local file path - resource does not use 'file:' protocol.
Now the project is on D drive, I am wondering why this is pointing C drive?
Also how can I add the specified hbm file in the mapping resource? This file is set as 'Embedded Resource'.
Is there any way to specify a directory here to scan for the multiple hbm files?
Any information will be very helpful to me.
You are not using a protocol identifier (e.g. file:// or assembly://) in your resource name, so Spring uses the default file protocol. It tries to resolve the hbm file from the location where the web app is running (the devserver path), not where it's files are stored, which you seem to expect.
To reference a file in your website use a ~ to identify the root of your web site, e.g. <value>file://~/EMSApplication.Domain/EMSApplication.hbm.xml</value>. But make sure the file is copied, which might not be the case, since you have set it to embedded resource (why?).
Consider the following improvements, they'll make your live more easy:
move the code in your folders dao, domain and NHibernate to a separate assembly (a class library project that is referenced from your web application) named Your.AssemblyName.
The session factory also supports specifying mapping assemblies to scan, so that you don't have to specify each file - see the docs on setting up a session factory using spring.net. Specify Your.AssemblyName in the mapping assembly list:
<object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Your.AssemblyName</value>
</list>
</property>
<property name="HibernateProperties">
<!-- snip -->
</object>
And check out the NHibernate example in the spring docs - it's very applicable to the questions you are raising here.
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");