NHibernate and sqlite - Could not compile the mapping document - nhibernate

I'm trying to run an NHibernate over sqlite.
i have two projects:
1. Orange.Database - holds the pocos and daos and everything else
2. Orange.GUI - holds the gui...
when the program reach to the reach the:
Configuration config = new Configuration();
config.AddAssembly("Orange.Database");
sessionFactory = config.Configure().BuildSessionFactory();
an exception is thrown: "Could not compile the mapping document: Orange.Database.Pocos.City.hbm.xml "
inner exception: "Could not find the dialect in the configuration"
city.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Orange.Database.Pocos">
<class name="City" table="Cities">
<id name="Id">
<column name="Id" sql-type="int" not-null="true"/>
<generator class="identity"/>
</id>
<property name="Name">
<column name="name" not-null="true"/>
</property>
<property name="IsTaxFree">
<column name="is_tax_free" not-null="true"/>
</property>
</class>
</hibernate-mapping>
I tried writting the assembly, and then removed it..
the app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
<property name="connection.connection_string">
Data Source=C:\Users\Nadav\Documents\Visual Studio 2005\Projects\orange\DB\OrangeDB\OrangeDB.db;Version=3
</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
</session-factory>
</hibernate-configuration>
</configuration>
I've tried different location of the db file..
i have tried to remove the configSections
and some other ideas i found on the web...
I'm using vs 2005
NHibernate version is 2.0.1.4000
Any suggestions?

Do this in your code:
Configuration config = new Configuration();
config.Configure();
config.AddAssembly("Orange.Database");
sessionFactory = config.BuildSessionFactory();

First, download the latest stable version of NHibernate, 2.1.2.
Second, try creating a configuration file named hibernate.cfg.xml, containing:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
<property name="connection.connection_string">
Data Source=C:\Users\Nadav\Documents\Visual Studio 2005\Projects\orange\DB\OrangeDB\OrangeDB.db;Version=3
</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
</session-factory>
</hibernate-configuration>

I got the same "could not compile error" but I'm not using sqlite.
I got it because I was calling the configure method from the nhibernate configuration instance twice.

Related

Display Hibernate SQL To Console (Spring)

I'm working with spring 3, hibernate 4. I'm trying to follow this tutorial http://www.mkyong.com/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/, but my hibernate configuration is different:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
username root and blank password. Change below if it's not the case -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/project"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="show_sql" value="true" />
<property name="format_sql" value="true" />
<property name="use_sql_comments" value="true" />
</bean>
</beans>
And the properties show_sql, format_sql and use_sql_comments are not working this way. I get this exception:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'show_sql' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'show_sql' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Is there anyway to achieve the tutorial with the definition of the bean??
show_sql not a property of org.apache.commons.dbcp.BasicDataSource . You have to define it in session factory configuration.
like this
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="data" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
The simplest approach probably is to set following logger to DEBUG:
org.hibernate.SQL
If you use log4j, find / create a log4j.properties file on your classpath root and add
log4j.logger.org.hibernate.SQL=DEBUG
See here for more info about log4j properties: http://logging.apache.org/log4j/1.2/manual.html
As I'm using JEE 8 and JBoss EAP, I managed to got the SQL after adding this line:
-Dorg.jboss.as.logging.per-deployment=false
on the end of "VM arguments" (Server tab -> JBoss Properties -> Open lauch configuration).

NHibernate component loading as null when many-to-one NULL, despite several non-null properties

I understand that when all the properties of a mapped component are null in the database, NHibernate will set the component to null. However, in the following case the component is being set to null if the many-to-one item LevelOfInvolvement is null, despite the fact that all the other properties of the component are not null.
Changing the value of the LevelOfInvolvement column in the database will cause the component to be null or not, regardless of the value of the other fields.
Here is the maping in full:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHS.WebTeam.LPTInvolvementHub.Activity, NHS.WebTeam.LPTInvolvementHub" table="[Activity]" lazy="false" optimistic-lock="version" where="Deleted=0" >
<id name="ID" type="Int32" column="ID" unsaved-value="0">
<generator class="hilo">
<param name="table">NHibernateHiLo</param>
<param name="column">NextValue</param>
<param name="max_lo">100</param>
</generator>
</id>
<version name="Version"/>
<property name="Deleted" />
<property name="LastUpdateBy" />
<property name="LastUpdateDate" />
<many-to-one name="Service" column="ServiceID"></many-to-one>
<property name="Title"></property>
<property name="Abstract"></property>
<property name="ProposedDate"></property>
<property name="DueDate"></property>
<property name="ActualDate"></property>
<property name="Rationale"></property>
<many-to-one name="PreAssessment" column="PreAssessmentID"></many-to-one>
<many-to-one name="PostAssessment" column="PostAssessmentID"></many-to-one>
<component name="InvolvementChecklist">
<property name="Impact" column="InvolvementChecklist_Impact"></property>
<property name="RewardsAndRecognition" column="InvolvementChecklist_RewardsAndRecognition"></property>
<property name="Training" column="InvolvementChecklist_Training"></property>
<property name="LogisticalIssues" column="InvolvementChecklist_LogisticalIssues"></property>
<property name="Feedback" column="InvolvementChecklist_Feedback"></property>
<property name="DueRegard" column="InvolvementChecklist_DueRegard"></property>
<property name="SupportRequiredFromTheTeam" column="InvolvementChecklist_SupportRequiredFromTheTeam"></property>
<property name="QualityAssurance" column="InvolvementChecklist_QualityAssurance"></property>
<property name="NonComplianceReason" column="InvolvementChecklist_NonComplianceReason"></property>
<many-to-one name="LevelOfInvolvement" column="InvolvementChecklist_LevelOfInvolvementID"></many-to-one>
</component>
</class>
</hibernate-mapping>
The code to load the entity is simply:
entity = NHibernateSession.Load(persitentType, id);
I am using NHibernate version 3.3.1
This seems a relatively simple mapping, but I cannot find any reference to others having similar problems.
In addition to returning null if all of the component members are null, I believe NHibernate will also treat a component as null if any non-nullable members are null. By default a <many-to-one> mapping creates a non-nullable member, which I think would cause the behaviour you're seeing. Try changing the LevelOfInvolement mapping to allow nulls:
<many-to-one
name="LevelOfInvolvement"
column="InvolvementChecklist_LevelOfInvolvementID"
not-null="false" />

How to get nhibernate to cache tables referenced via many-to-one - is my config correct?

I've been trying to get this working for a while now with no luck. I want to enable the 2nd level cache to prevent fetching from some lookup tables.
I set up my configuration in code
cfg = new Configuration();
cfg.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider";
string connectionString = connection.ConnectionString;
cfg.Properties[NHibernate.Cfg.Environment.ConnectionString] = connectionString;
cfg.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.Driver.SqlClientDriver";
cfg.Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.Dialect.MsSql2005Dialect";
cfg.Properties[NHibernate.Cfg.Environment.CommandTimeout] = "720";
cfg.Properties[NHibernate.Cfg.Environment.CacheProvider] = "NHibernate.Cache.HashtableCacheProvider";
cfg.Properties[NHibernate.Cfg.Environment.UseSecondLevelCache] = "true";
cfg.Properties[NHibernate.Cfg.Environment.UseQueryCache] = "true";
cfg.AddAssembly("APPName.PersistentEntities");
factory = cfg.BuildSessionFactory();
Then in my xml configuration I added the cache attribute:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="APPName.PersistentEntities.LockStatus, APPName.PersistentEntities" table="LockStatus" lazy="false">
<meta attribute="class-description">lockstatus</meta>
<cache usage="read-only" />
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="Name" column="Name" type="String" length="100" not-null="true"/>
</class>
</hibernate-mapping>
This entity is then referenced from other uncached entities
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="APPName.PersistentEntities.Entity, APPName.PersistentEntities" table="Entity" lazy="false">
<meta attribute="class-description">entity</meta>
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="Name" column="Name" type="String" length="500" not-null="true">
<meta attribute="field-description">name</meta>
</property>
<many-to-one name="LockStatus" column="LockStatusId" class="APPName.PersistentEntities.LockStatus, APPName.PersistentEntities" not-null="false" lazy="false">
</many-to-one>
</class>
</hibernate-mapping>
>
I query this other entity with something like:
session = factory.OpenSession();
IList<T> s = session.CreateSQLQuery(query).AddEntity(typeof(T)).SetCacheable(true).List<T>();
session.Clear();
session.Close();
The query and mappings run fine so to make sure its using the cache I try updating the names in the database. When I click again in the application I see the updated names so I assume it is not using the cache re-querying.
you need to add a cache declaration on the relation as well (LockStatus).
also-
you can use nHibernate's logging to see exactly the sql sent on every call.
I don't see why you'd want to use an SQLQuery in your case; you can simply use Query or QueryOver.

Configuring Fluent NHibernate from NHibernate config section

I'm trying to use Fluent NHibernate in my solution by configuring it with the following NHibernate xml configuration section
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="mitre">
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">Data Source=YOUR_DB_SERVER;Database=Northwind;User ID=YOUR_USERNAME;Password=YOUR_PASSWORD;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">TRATE</property>
<!-- HBM Mapping Files -->
<mapping assembly="Markel.Mint.Mitre.Data" />
</session-factory>
</hibernate-configuration>
In my code file, to instantiate ISession:
NH_Cfg.Configuration cfg = new NH_Cfg.Configuration();
cfg.Configure();
Fluently.Configure(cfg).Mappings(m => m.FluentMappings = ????)
My question is that if I have already specified the assembly in the NHibernate config section, do I need to explicitly set FluentMappings? If so, then is it possible to retrieve this data from NHibernate config programmatically?
Thanks
Oz
The mapping assembly in hibernate.cfg.xml is searched for embedded *.hbm.xml files. NHibernate does not know anything about fluent mappings (e.g. ClassMap) as those are introduced by Fluent NHibernate. So you need:
Fluently.Configure(cfg).Mappings(m => m.FluentMappings.AddFromAssemblyOf<SomeDomainType>();
in order to configure NHibernate using your ClassMap mappings.
Thanks for the quick response, James.
Could I do the following then?
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="mitre">
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">Data Source=YOUR_DB_SERVER;Database=Northwind;User ID=YOUR_USERNAME;Password=YOUR_PASSWORD;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">TRATE</property>
<property name="fluent.nhibernate.fluentmapping">Markel.Mint.Mitre.Core.Domain</property>
</session-factory>
</hibernate-configuration>
Then my code could refer to the property thus:
NH_Cfg.Configuration cfg = new NH_Cfg.Configuration(); cfg.Configure();
Fluently.Configure(cfg).Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load(cfg.Properties["fluent.nhibernate.fluentmapping"])));

NHibernate QuerySyntaxException

I am following along with the Summer of NHibernate Screencast Series and am running into a strange NHibernate Exception.
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException:
Exception of type
'Antlr.Runtime.NoViableAltException' was thrown.
[select from DataTransfer.Person p where p.FirstName=:fn].
I have deviated from the Screencast Series in the following ways:
Running against an MS SQL Server Compact Database
I am using MSTest instead of MbUnit
I've tried any number of combination of queries always with the same result. My present CreateQuery syntax
public IList<Person> GetPersonsByFirstName(string firstName)
{
ISession session = GetSession();
return session.CreateQuery("select from Person p " +
"where p.FirstName=:fn").SetString("fn", firstName)
.List<Person>();
}
While not a direct query this method works
public Person GetPersonById(int personId)
{
ISession session = GetSession();
return session.Get<Person>(personId);
}
My hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="BookDb">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.connection_string">Data Source=C:\Code\BookCollection\DataAccessLayer\BookCollectionDb.sdf</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="DataTransfer"/>
</session-factory>
</hibernate-configuration>
Person.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
<class name="DataTransfer.Person,DataTransfer" table="Person">
<id name="PersonId" column="PersonId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="FirstName" column="FirstName" type="String" length="50" not-null="false" />
<property name="LastName" column="LastName" type="String" length="50" not-null="false" />
</class>
</hibernate-mapping>
I was also following the Summer of NHibernate Screencast Series and came across the same problem.
The problem is in the HQL "select from User p" change that to "select p from User p" or just "from User p".
The ’shorthand’ HQL form that was used in the screencasts under NHibernate version 1.2 was deprecated in 2.0 and eliminated in 2.1.x as the default query parser was switched out to be the more strict option.
public IList<Person> GetPersonsByFirstName(string firstName)
{
ISession session = GetSession();
return session.CreateQuery("select p from Person p where p.FirstName=:fn")
.SetString("fn", firstName)
.List<Person>();
}
Since you're specifying the namespace in the <hibernate-mapping element, you could write :
<class name="Person" table="Person">
....
After you try that, if it doesn't work - I have no idea why it isn't working. I've tried pretty much the example you gave and it worked .
I have seen the new parser throw some weird errors and you just have to go by trial and error when it happens :(.
Edit
About the trial and error : you could change the query to "from Person" see if that works(if it doesn't...i'm stuck ) . Then add the filter, first try directly p.FirstName = 'x'. Then try with parameter. You could try not adding the alias.
Also, try using the latest version of NH.
Edit 2
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" namespace="NHibernateTests">
<class name="User" table="`User`" xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" type="Int32" column="UserId">
<generator class="assigned" />
</id>
<property name="UserName" type="String">
<column name="UserName" not-null="true" />
</property>
<property name="FName" type="String">
<column name="FName" />
</property>
</class></hibernate-mapping>
and the query :
IList<User> users = session.CreateQuery("select from User p " +
"where p.UserName=:fn").SetString("fn", "u")
.List<User>();
Worked like a charm.