Hello, I'm new to NHibernate. I'm working on an application where my Mapping Files, my mapped classes and the database configuration class are in a classe library that I import to a client app.I've got 2 diferrent problems:
1- I started using mapping files but some contain the <bag> tag. So, when I build my app, it shows m an errr saying that the <bag> tag must not be a chid of <class> tag. what should I do? Can it be replcaed with another tag?
2- As I couldn't use mapping files, I started using Mapping Attributes and I got other errors when executing queries, such as:
Quote:
An unhandled exception of type 'NHibernate.QueryException' occurred in NHibernate.dll
Additional information: in expected: <end-of-text> (possibly an invalid or unmapped class name was used in the query)
this are my files:
Code:
public sealed class Handler
{
private ISessionFactory sessionFactory;
/// <summary>
/// Constructeur Paramétré, on y configure également NHibernate
/// </summary>
/// <param name="bdd">Database Name</param>
/// <param name="cnxStr">Database Connection String</param>
public Handler(string cnxStr)
{
// Activation de l'inscription des informations de débogages sur les opérations de NHibernate
//XmlConfigurator.Configure();
Configuration cfg = new Configuration();
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2000Dialect");
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, "NHibernate.Driver.SqlClientDriver");
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, cnxStr);
// Utilise NHibernate.Mapping.Attributes pour créer les informations sur les entités
MemoryStream flux = new MemoryStream(); // Contenant des informations
HbmSerializer.Default.Validate = true; // Active la validation (optionnel)
// Demande à NHibernate d'utiliser les champs et non les propriétés (dans les entités)
HbmSerializer.Default.HbmDefaultAccess = "field.camelcase";
// Récupère les informations à partir de cette assemblée (peut aussi être fait classe par classe)
HbmSerializer.Default.Serialize(flux, System.Reflection.Assembly.GetExecutingAssembly());
flux.Position = 0;
cfg.AddInputStream(flux); // Envoi les informations de Mappage à la Configuration de NHibernate
flux.Close();
// Construit la "fabrique de sessions"
sessionFactory = cfg.BuildSessionFactory();
}
/// <summary>
/// Affichage de toutes les données de la table Domaines
/// </summary>
/// <returns>Une liste de données</returns>
public IList PrintAll()
{
ISession session = sessionFactory.OpenSession();
IList list;
//try
//{
IQuery cq = session.CreateQuery("from MAGASINS");
list = cq.List();
session.Close();
//}
//catch
//{
// throw new HibernateException("Unable to print all entries...");
//}
return list;
}
Code:
[NHibernate.Mapping.Attributes.Class(Table = "MAGASINS")]
public class Magasins
{
#region Private Members
private bool isChanged;
private string codeMagasin;
//private IList AdrMagList;
private string libMagasin;
#endregion
#region Default ( Empty ) Class Constuctor
/// <summary>
/// default constructor
/// </summary>
public Magasins()
{
}
#endregion // End of Default ( Empty ) Class Constuctor
#region Required Fields Only Constructor
/// <summary>
/// required (not null) fields only constructor
/// </summary>
public Magasins(string libMagasin)
{
this.libMagasin = libMagasin;
}
#endregion // End Required Fields Only Constructor
#region Public Properties
/// <summary>
///
/// </summary>
[NHibernate.Mapping.Attributes.Id(Name = "CodeMagasin")]
[NHibernate.Mapping.Attributes.Generator(1, Class = "assigned")]
public virtual string CodeMagasin
{
get
{
return this.codeMagasin;
}
set
{
if( value == null )
throw new ArgumentOutOfRangeException("Null value not allowed for CodeMagasin", value, "null");
if( value.Length > 20)
throw new ArgumentOutOfRangeException("Invalid value for CodeMagasin", value, value.ToString());
this.codeMagasin = value;
}
}
//public IList AdrMagList
//{
// get
// {
// return AdrMagList;
// }
// set
// {
// AdrMagList = value;
// }
//}
/// <summary>
///
/// </summary>
[NHibernate.Mapping.Attributes.Property(Name = "LibMagasin", Column = "LIB_MAGASIN")]
public virtual string LibMagasin
{
get
{
return this.libMagasin;
}
set
{
if( value != null && value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for LibMagasin", value, value.ToString());
this.libMagasin = value;
}
}
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public virtual bool IsChanged
{
get
{
return isChanged;
}
}
#endregion
}
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Kpf.CoucheMetier.Magasins,Kpf.CoucheMetier" table="MAGASINS">
<id name="CodeMagasin" column="CODE_MAGASIN" type="String">
<generator class="assigned"/>
</id>
<bag name="AdrMagList" inverse="true" lazy="true" >
<key column="CODE_MAGASIN" />
<one-to-many class="Kpf.CoucheMetier.AdrMag,Kpf.CoucheMetier" />
</bag>
<property column="LIB_MAGASIN" type="String" name="LibMagasin" length="50" />
</class>
</hibernate-mapping>
Thanks for your help, I'm really in need