Hi, i have a problem getting the hbm2ddl tool working. I tried several ways described in the test code and on the old forum, but no luck. Considering i am new at .net, the problem migt be something related to .net instead of nhibernate but i ask you to bear with me. I have however several years of java programming experiance and have worked with (java) hibernate including the hbm2ddl tool.
So much for the lengthy introduction, on to the problem ;)
I am trying to add assemblies dynamicly too the hibernate mapping and then generate the required tables for it. imagin an application that shows a list of products, if i were to tell the application: here is a dll with additional products use it, i would expext the next time it shows the list it also contains the new products. all this without restarting the app.
I created a little test (1 solution with 2 projects (visual studio 2003)) to test this, the first project (CodeGeneration) only refferences the tool and nhibernate. the 2nd project (DataObjects) contains no references, 1 hbm file and 1 c# classfile for the hbm. the 2nd project is compiled into a dll (hbm included as resource) and then at runtime given to the main app in the first project like this
Code:
using System;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
namespace CodeGeneration
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
[STAThread]
public static void Main(string[] args)
{
try
{
Configuration cfg = new Configuration();
ISessionFactory factory = null;
Assembly assm=Assembly.LoadFile(@"D:\.Net Projects\hbm2ddlTest\DataObjects\bin\debug\DataObjects.dll");
//Type T =assm.GetType("DataObjects.Status"); // this works
//T=System.Type.GetType("DataObjects.Status"); // this doesnt, and this is how nhibernate tries to load the class
//cfg.AddAssembly(assm); //does not work
// Type[] T=assm.GetTypes();
// for(int i=0;i<T.Length;i++)
// cfg.AddClass(T[i]);
//also does not work tries to use System.Type.getType again
cfg.AddResource("DataObjects.Status.hbm.xml",assm); //doesnt work
factory = cfg.BuildSessionFactory();
NHibernate.Tool.hbm2ddl.SchemaExport sex = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
sex.SetOutputFile(@"D:\.Net Projects\hbm2ddlTest\CodeGeneration\schema.sql");
sex.Create(false,false);
// no sex is not the only thing on my mind ;) this was copied from the old forum
}
catch(Exception err)
{
System.Console.Error.WriteLine(err.ToString());
System.Console.Error.WriteLine(err.StackTrace.ToString());
}
}
}
}
this is the hbm file
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="DataObjects.Status, DataObjects" table="Status">
<id name="StatusCode" column="StatusCode" type="String(5)">
<generator class="native" />
</id>
<property name="Omschrijving" column="Omschrijving" type="String(60)"/>
<property name="Eindpunt" column="Eindpunt" type="Boolean"/>
<property name="BoekingStatus" column="Boekingsstatus" type="Boolean"/>
<property name="VolgordeNummer" column="volgorde_id" type="Int32"/>
<property name="Actief" column="Active" type="Boolean"/>
</class>
</hibernate-mapping>
and this is the class that was generated for the hbm by the hbm2net tool
Code:
using System;
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: v1.1.4322
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
namespace DataObjects
{
/// <summary>
/// POJO for DataObjects.Status
/// </summary>
/// <remark>
/// This class is autogenerated
/// </remark>
public class Status {
public static readonly string STR_STATUSCODE = "StatusCode";
public static readonly string STR_OMSCHRIJVING = "Omschrijving";
public static readonly string STR_EINDPUNT = "Eindpunt";
public static readonly string STR_BOEKINGSTATUS = "BoekingStatus";
public static readonly string STR_VOLGORDENUMMER = "VolgordeNummer";
public static readonly string STR_ACTIEF = "Actief";
/// <summary>
/// Holder for identifier field StatusCode
/// </summary>
private String statusCode;
/// <summary>
/// Holder for nullable persistent field Omschrijving
/// </summary>
private String omschrijving;
/// <summary>
/// Holder for nullable persistent field Eindpunt
/// </summary>
private Boolean eindpunt;
/// <summary>
/// Holder for nullable persistent field BoekingStatus
/// </summary>
private Boolean boekingStatus;
/// <summary>
/// Holder for nullable persistent field VolgordeNummer
/// </summary>
private Int32 volgordeNummer;
/// <summary>
/// Holder for nullable persistent field Actief
/// </summary>
private Boolean actief;
/// <summary>
/// full constructor
/// </summary>
public Status(String Omschrijving, Boolean Eindpunt, Boolean BoekingStatus, Int32 VolgordeNummer, Boolean Actief)
{
this.Omschrijving = Omschrijving;
this.Eindpunt = Eindpunt;
this.BoekingStatus = BoekingStatus;
this.VolgordeNummer = VolgordeNummer;
this.Actief = Actief;
}
/// <summary>
/// default constructor
/// </summary>
public Status() {
}
public String StatusCode
{
get { return this.statusCode; }
set
{
this.statusCode = value;
}
}
public String Omschrijving
{
get { return this.omschrijving; }
set
{
this.omschrijving = value;
}
}
public Boolean Eindpunt
{
get { return this.eindpunt; }
set
{
this.eindpunt = value;
}
}
public Boolean BoekingStatus
{
get { return this.boekingStatus; }
set
{
this.boekingStatus = value;
}
}
public Int32 VolgordeNummer
{
get { return this.volgordeNummer; }
set
{
this.volgordeNummer = value;
}
}
public Boolean Actief
{
get { return this.actief; }
set
{
this.actief = value;
}
}
public override string ToString()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("statusCode={0} ", statusCode);
return sb.ToString();
}
}
}
The problem seems to be that it cannot find the Status class belonging to the hbm, attemps to feed the configuration the class file have also failed.
i am using nhibernate 0.8.3.0
Any help / insight is greatly appreciated
I will be happy to provide the project files for those interested.
For the record i have been able to generate a schema if i made a reference to the second project in the first project but that is what i am trying to avoid here.