Is it possible to map public fields? When I try to map some class which inherits public fields (not properties) I get NullReferenceException.
It’s a big problem when I have to use some class which I didn’t write and I don’t have sources.
Example:
Classes:
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace QuickStart
{
//some class
public class Cat
{
private string id;
public string Name;//everything works fine when I change public to protection
private char sex;
private float weight;
public Cat()
{
Name = "test";
}
public virtual string Id
{
get { return id; }
set { id = value; }
}
public virtual char Sex
{
get { return sex; }
set { sex = value; }
}
public virtual float Weight
{
get { return weight; }
set { weight = value; }
}
}
//my class
public class SuperCat: Cat
{
private int speed;
public SuperCat()
{
}
public virtual int Speed
{
get { return speed; }
set { speed = value; }
}
}
}
Map file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="QuickStartLibs" namespace="QuickStart" auto-import="true" default-access="nosetter.camelcase">
<class name="SuperCat" table="Cat" >
<id name="Id">
<column name="CatId" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<property name="Name" access="field">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<property name="Weight" />
<property name="Speed" />
</class>
</hibernate-mapping>
Program:
Code:
namespace QuickStart
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
try
{
XmlConfigurator.Configure();
NHibernateSessionFactoryBuilder nsfb = NHibernateSessionFactoryBuilder.GetNHibernateSessionFactoryBuilder();
ISession session = nsfb.GetSession();
ITransaction tx = session.BeginTransaction();
SuperCat prince = new SuperCat();
// prince.Name = "Princess";
prince.Sex = 'F';
prince.Weight = 8.4f;
prince.Speed = 100;
session.Save(prince);
tx.Commit();
session.Close();
}
catch (Exception ex)
{
log.Error("Wystąpił błąd", ex);
}
}
}
}
Logs:
Code:
2007-07-04 08:57:40,562 [10] INFO NHibernate.Cfg.Environment - NHibernate 1.2.0.4000 (1.2.0.4000)
2007-07-04 08:57:40,593 [10] INFO NHibernate.Cfg.Environment - Bytecode provider name : lcg
2007-07-04 08:57:40,593 [10] INFO NHibernate.Cfg.Environment - Using reflection optimizer
2007-07-04 08:57:40,593 [10] INFO NHibernate.Cfg.Configuration - Searching for mapped documents in assembly: QuickStartLibs
2007-07-04 08:57:40,609 [10] INFO NHibernate.Cfg.Configuration - Adding embedded mapping document: QuickStart.Mappings.Cat.hbm.xml
2007-07-04 08:57:40,609 [10] INFO NHibernate.Cfg.Configuration - Mapping resource: QuickStart.Mappings.Cat.hbm.xml
2007-07-04 08:57:40,656 [10] INFO NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2005Dialect
2007-07-04 08:57:40,687 [10] INFO NHibernate.Cfg.HbmBinder - Mapping class: QuickStart.SuperCat -> Cat
2007-07-04 08:57:40,718 [10] DEBUG NHibernate.Cfg.HbmBinder - Mapped property: Id -> CatId, type: String
2007-07-04 08:57:40,734 [10] DEBUG NHibernate.Cfg.HbmBinder - Mapped property: Name -> Name, type: String
2007-07-04 08:57:40,734 [10] DEBUG NHibernate.Cfg.HbmBinder - Mapped property: Sex -> Sex, type: Char
2007-07-04 08:57:40,734 [10] DEBUG NHibernate.Cfg.HbmBinder - Mapped property: Weight -> Weight, type: Single
2007-07-04 08:57:40,734 [10] DEBUG NHibernate.Cfg.HbmBinder - Mapped property: Speed -> Speed, type: Int32
2007-07-04 08:57:40,734 [10] INFO NHibernate.Cfg.Configuration - processing one-to-many association mappings
2007-07-04 08:57:40,734 [10] INFO NHibernate.Cfg.Configuration - processing one-to-one association property references
2007-07-04 08:57:40,734 [10] INFO NHibernate.Cfg.Configuration - processing foreign key constraints
2007-07-04 08:57:40,750 [10] ERROR QuickStart.Program - Błąd
System.NullReferenceException