Hello,
I am trying to build a simple POCO that persists to NHibernate using Attributes. I am using Mono, Sqllite, NHibernate 1.2.0beta3 on Ubuntu.
It took me a while to figure out the NHibernate Sqlite driver doesn't use Mono, so I use this code:
Code:
namespace LightTable.Core
{
using NHibernate.Driver;
public class MonoDataSqliteDriver : ReflectionBasedDriver
{
public MonoDataSqliteDriver() : base(
"Mono.Data.SqliteClient",
"Mono.Data.SqliteClient.SqliteConnection",
"Mono.Data.SqliteClient.SqliteCommand")
{
}
public override bool UseNamedPrefixInSql
{
get { return true; }
}
public override bool UseNamedPrefixInParameter
{
get { return true; }
}
public override string NamedPrefix
{
get { return "@"; }
}
public override bool SupportsMultipleOpenReaders
{
get { return false; }
}
}
}
My .config contains the following:
Code:
<nhibernate>
<add key="hibernate.dialect"
value="NHibernate.Dialect.SQLiteDialect" />
<add key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.driver_class"
value="LightTable.Core.MonoDataSqliteDriver, LightTableCore"/>
<add key="hibernate.connection.connection_string"
value="URI=file:nhibernate.db,version=3" />
<add key="hibernate.query.substitutions"
value="true=1;false=0" />
</nhibernate>
When I call SchemaExport, I can see my driver being instantiated and the tables being created from my annotated classes. My code looks as such:
Code:
public static void Init()
{
log4net.Config.XmlConfigurator.Configure();
MemoryStream stream = new MemoryStream();
HbmSerializer.Default.Validate = true;
HbmSerializer.Default.HbmDefaultAccess = "field.camelcase-underscore";
HbmSerializer.Default.Serialize(stream,
System.Reflection.Assembly.GetExecutingAssembly());
stream.Position = 0;
System.Console.WriteLine(new StreamReader(stream).ReadToEnd());
stream.Position = 0;
Configuration cfg = new Configuration();
cfg.AddInputStream(stream);
NHibernate.Tool.hbm2ddl.SchemaExport se = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
se.Create(true, true);
NHibernate.ISessionFactory sf = cfg.BuildSessionFactory();
However when I try to create a ISessionFactory, the call throws this exception:
Code:
Unhandled Exception: System.ArgumentException: Name can't be empty
Parameter name: name
at System.Reflection.Emit.DynamicMethod..ctor (System.String name, MethodAttributes attributes, CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Reflection.Module m, Boolean skipVisibility) [0x000ff] in /build/buildd/mono-1.1.17.1/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs:107
at System.Reflection.Emit.DynamicMethod..ctor (System.String name, MethodAttributes attributes, CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Type owner, Boolean skipVisibility) [0x00000] in /build/buildd/mono-1.1.17.1/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs:80
at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, Boolean skipVisibility) [0x00000] in /build/buildd/mono-1.1.17.1/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs:77
at NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateDynamicMethod (System.Type returnType, System.Type[] argumentTypes) [0x00000]
at NHibernate.Bytecode.Lightweight.ReflectionOptimizer.GenerateGetPropertyValuesMethod (NHibernate.Property.IGetter[] getters) [0x00000]
at NHibernate.Bytecode.Lightweight.ReflectionOptimizer..ctor (System.Type mappedType, NHibernate.Property.IGetter[] getters, NHibernate.Property.ISetter[] setters) [0x00000]
at NHibernate.Bytecode.Lightweight.BytecodeProviderImpl.GetReflectionOptimizer (System.Type mappedClass, NHibernate.Property.IGetter[] getters, NHibernate.Property.ISetter[] setters) [0x00000]
at NHibernate.Persister.Entity.AbstractEntityPersister..ctor (NHibernate.Mapping.PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory) [0x00000]
at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor (NHibernate.Mapping.PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) [0x00000]
at NHibernate.Persister.PersisterFactory.CreateClassPersister (NHibernate.Mapping.PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) [0x00000]
at NHibernate.Impl.SessionFactoryImpl..ctor (NHibernate.Cfg.Configuration cfg, IMapping mapping, NHibernate.Cfg.Settings settings) [0x00000]
at NHibernate.Cfg.Configuration.BuildSessionFactory () [0x00000]
at LightTable.Core.Initialize.Init () [0x00074] in /home/george/Projects/dam/src/Core/Initialize.cs:34
at LightTable.Core.Test.Main (System.String[] args) [0x00000] in /home/george/Projects/dam/src/Core/test.cs:7
My generated XML is fairly simple:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!--Generated from NHibernate.Mapping.Attributes on 2006-11-27 01:09:29Z.-->
<hibernate-mapping default-access="field.camelcase-underscore" xmlns="urn:nhibernate-mapping-2.2">
<class name="LightTable.Core.Media, LightTableCore" table="Media" polymorphism="implicit">
<id name="Location">
<generator class="assigned" />
</id>
</class>
</hibernate-mapping>
Is there something I am missing? I'm very new to NHibernate.