复杂表关联时,使用NHibernate需要注意些什么
实体文件和单元测试文件如下,在运行单元测试文件时,无法通过,提示:associated class not found无法找到TInformFailure类.
我想问问,由于实体之间关系复杂(一对多、多对一),是否在测试文件中cfg.AddClass()时需要考虑顺序文件,或者要加入一些表面上看来与要测试的实体类无关的其它类?
// 实体文件
using System;
using System.Collections;
namespace LyDms.Persistence.Data
{
public class TAgency
{
private IList _tcharges; // TCharge
private IList _tinformFailures; // TInformFailure
private IList _trecroupGas; // TRecroupGas
private IList _tchecks; // TCheck
private IList _trecroupMoneys; // TRecroupMoney
private IList _treleaseCards; // TReleaseCard
private IList _tcancelUsers; // TCancelUser
private IList _trepairInfos; // TRepairInfo
private IList _topenUsers; // TOpenUser
public TAgency()
{
}
public IList tcharges
{
get { return _tcharges; }
set { _tcharges = value; }
}
public IList tinformFailures
{
get { return _tinformFailures; }
set { _tinformFailures = value; }
}
public IList trecroupGas
{
get { return _trecroupGas; }
set { _trecroupGas = value; }
}
public IList tchecks
{
get { return _tchecks; }
set { _tchecks = value; }
}
public IList trecroupMoneys
{
get { return _trecroupMoneys; }
set { _trecroupMoneys = value; }
}
public IList treleaseCards
{
get { return _treleaseCards; }
set { _treleaseCards = value; }
}
public IList tcancelUsers
{
get { return _tcancelUsers; }
set { _tcancelUsers = value; }
}
public IList trepairInfos
{
get { return _trepairInfos; }
set { _trepairInfos = value; }
}
public IList topenUsers
{
get { return _topenUsers; }
set { _topenUsers = value; }
}
// native properties
private System.String _FAgencyName;
public System.String FAgencyName
{
get { return _FAgencyName; }
set { _FAgencyName = value; }
}
private System.String _FAgencyCode;
public System.String FAgencyCode
{
get { return _FAgencyCode; }
set { _FAgencyCode = value; }
}
private System.String _FAgencyType;
public System.String FAgencyType
{
get { return _FAgencyType; }
set { _FAgencyType = value; }
}
private System.String _FAgencyDes;
public System.String FAgencyDes
{
get { return _FAgencyDes; }
set { _FAgencyDes = value; }
}
private System.Int32 _FAgencyWC;
public System.Int32 FAgencyWC
{
get { return _FAgencyWC; }
set { _FAgencyWC = value; }
}
}
}
// 单元测试文件
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using LyDms.Persistence.Data;
namespace LyDms.Persistence.Test.Data
{
[TestFixture]
public class TAgencyFixture
{
public TAgencyFixture()
{
}
[Test]
public void TestAddAgency()
{
Configuration cfg = new Configuration();
//cfg.AddAssembly("LyDms.Persistence.Data");
cfg.AddClass(typeof(TAgency));
cfg.AddClass(typeof(TCharge));
cfg.AddClass(typeof(TInformFailure));
cfg.AddClass(typeof(TRecroupGas));
cfg.AddClass(typeof(TCheck));
cfg.AddClass(typeof(TRecroupMoney));
cfg.AddClass(typeof(TReleaseCard));
cfg.AddClass(typeof(TCancelUser));
cfg.AddClass(typeof(TRepairInfo));
cfg.AddClass(typeof(TOpenUser));
new SchemaExport (cfg).Create(true,true);
ISessionFactory sessions = cfg.BuildSessionFactory();
ISession sn = sessions.OpenSession();
ITransaction tx = null;
TAgency agy = new TAgency();
agy.FAgencyCode = "01";
agy.FAgencyDes = "湖北省武汉市";
agy.FAgencyName = "阿门";
agy.FAgencyType = "1";
agy.FAgencyWC = 1001;
agy.tcharges = new ArrayList();
agy.tchecks = new ArrayList();
try
{
tx = sn.BeginTransaction();
sn.Save(agy);
tx.Commit();
}
catch (HibernateException e)
{
if ( tx != null )
tx.Rollback();
throw e;
}
finally
{
sn.Close();
}
}
}
}
我想问问,由于实体之间关系复杂(一对多、多对一),是否在测试文件中cfg.AddClass()时需要考虑顺序文件,或者要加入一些表面上看来与要测试的实体类无关的其它类?
// 实体文件
using System;
using System.Collections;
namespace LyDms.Persistence.Data
{
public class TAgency
{
private IList _tcharges; // TCharge
private IList _tinformFailures; // TInformFailure
private IList _trecroupGas; // TRecroupGas
private IList _tchecks; // TCheck
private IList _trecroupMoneys; // TRecroupMoney
private IList _treleaseCards; // TReleaseCard
private IList _tcancelUsers; // TCancelUser
private IList _trepairInfos; // TRepairInfo
private IList _topenUsers; // TOpenUser
public TAgency()
{
}
public IList tcharges
{
get { return _tcharges; }
set { _tcharges = value; }
}
public IList tinformFailures
{
get { return _tinformFailures; }
set { _tinformFailures = value; }
}
public IList trecroupGas
{
get { return _trecroupGas; }
set { _trecroupGas = value; }
}
public IList tchecks
{
get { return _tchecks; }
set { _tchecks = value; }
}
public IList trecroupMoneys
{
get { return _trecroupMoneys; }
set { _trecroupMoneys = value; }
}
public IList treleaseCards
{
get { return _treleaseCards; }
set { _treleaseCards = value; }
}
public IList tcancelUsers
{
get { return _tcancelUsers; }
set { _tcancelUsers = value; }
}
public IList trepairInfos
{
get { return _trepairInfos; }
set { _trepairInfos = value; }
}
public IList topenUsers
{
get { return _topenUsers; }
set { _topenUsers = value; }
}
// native properties
private System.String _FAgencyName;
public System.String FAgencyName
{
get { return _FAgencyName; }
set { _FAgencyName = value; }
}
private System.String _FAgencyCode;
public System.String FAgencyCode
{
get { return _FAgencyCode; }
set { _FAgencyCode = value; }
}
private System.String _FAgencyType;
public System.String FAgencyType
{
get { return _FAgencyType; }
set { _FAgencyType = value; }
}
private System.String _FAgencyDes;
public System.String FAgencyDes
{
get { return _FAgencyDes; }
set { _FAgencyDes = value; }
}
private System.Int32 _FAgencyWC;
public System.Int32 FAgencyWC
{
get { return _FAgencyWC; }
set { _FAgencyWC = value; }
}
}
}
// 单元测试文件
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using LyDms.Persistence.Data;
namespace LyDms.Persistence.Test.Data
{
[TestFixture]
public class TAgencyFixture
{
public TAgencyFixture()
{
}
[Test]
public void TestAddAgency()
{
Configuration cfg = new Configuration();
//cfg.AddAssembly("LyDms.Persistence.Data");
cfg.AddClass(typeof(TAgency));
cfg.AddClass(typeof(TCharge));
cfg.AddClass(typeof(TInformFailure));
cfg.AddClass(typeof(TRecroupGas));
cfg.AddClass(typeof(TCheck));
cfg.AddClass(typeof(TRecroupMoney));
cfg.AddClass(typeof(TReleaseCard));
cfg.AddClass(typeof(TCancelUser));
cfg.AddClass(typeof(TRepairInfo));
cfg.AddClass(typeof(TOpenUser));
new SchemaExport (cfg).Create(true,true);
ISessionFactory sessions = cfg.BuildSessionFactory();
ISession sn = sessions.OpenSession();
ITransaction tx = null;
TAgency agy = new TAgency();
agy.FAgencyCode = "01";
agy.FAgencyDes = "湖北省武汉市";
agy.FAgencyName = "阿门";
agy.FAgencyType = "1";
agy.FAgencyWC = 1001;
agy.tcharges = new ArrayList();
agy.tchecks = new ArrayList();
try
{
tx = sn.BeginTransaction();
sn.Save(agy);
tx.Commit();
}
catch (HibernateException e)
{
if ( tx != null )
tx.Rollback();
throw e;
}
finally
{
sn.Close();
}
}
}
}
浙公网安备 33010602011771号