gpt4 book ai didi

c# - Nhibernate 非空属性引用空值或 transient 值 Tec.Core.Model.Budget.oInvestmentCode

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:40 25 4
gpt4 key购买 nike

早上好

我是 NHibernate 的新手。我想在我的新项目中使用它,但是我遇到了外键引用的问题。我不确定为什么会出现此问题。

显示的错误消息是:not-null property references a null or transient value Tec.Core.Model.Budget.oInvestmentType

这是我的表架构:

enter image description here

这是我的映射文件

机场

机场.hbm.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Tec.Core.Model.Airport, Tec.Core" table="tblAirport" lazy="false">
<id name="ID" column="AirportID" unsaved-value="0">
<generator class="identity" />
</id>
<property name="AirportCode" column="AirportCode" />
<property name="AirportFullName" column="AirportFullName" />

<bag name="Budgets" table="tblBudget" inverse="true" cascade="save-update">
<key column="AirportID" />
<one-to-many class="Tec.Core.Model.Budget, Tec.Core" />
</bag>
</class>
</hibernate-mapping>

机场.cs

public class Airport:Entity<int> 
{
private string _airportCode="";
private string _airportFullName="";
private IList<Budget> _airportBudgets = new List<Budget>();

private Airport() { }
public Airport(string AirportCode, string AirportFullName) {
this._airportCode = AirportCode;
this._airportFullName = AirportFullName;
}
public string AirportCode {
get { return _airportCode; }
set { _airportCode = value; }
}
public string AirportFullName {
get { return _airportFullName; }
set { _airportFullName = value; }
}
public IList<Budget> Budgets {
get { return new List<Budget>(_airportBudgets).AsReadOnly(); }
protected set { _airportBudgets = value; }
}
public void AddBudget(Budget air_budget){
if (air_budget != null && !_airportBudgets.Contains(air_budget)){
air_budget.oAirport = this;
_airportBudgets.Add(air_budget);
}
}

public override int GetHashCode(){
return (GetType().FullName + "|" + _airportCode.GetHashCode()).GetHashCode();
}

}

投资代码

InvestmentCode.hbm.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Tec.Core.Model.InvestmentCode, Tec.Core" table="tblInvestmentCode" lazy="false">
<id name="ID" column="InvestmentCodeID" unsaved-value="0">
<generator class="identity" />
</id>
<property name="InvCode" column="InvestmentCode" />
<property name="InvCodeNote" column="InvestmentCodeNote" />

<bag name="Budgets" table="tblBudget" inverse="true" cascade="save-update">
<key column="InvestmentCodeID" />
<one-to-many class="Tec.Core.Model.Budget, Tec.Core" />
</bag>
</class>
</hibernate-mapping>

InvestmentCode.cs

public class InvestmentCode: Entity {

    private string _investmentCode="";
private string _investmentCodeNote="";
private IList<Budget> _lstinvestmentCodeBudgets = new List<Budget>();

private InvestmentCode() { }
public InvestmentCode(string investmentCode, string InvestmentCodeNote) {
this._investmentCode = investmentCode;
this._investmentCodeNote = InvestmentCodeNote ;
}
public string InvCode {
get { return _investmentCode ; }
set { _investmentCode = value; }
}
public string InvCodeNote {
get { return _investmentCodeNote ; }
set { _investmentCodeNote = value; }
}
public IList<Budget> Budgets {
get { return new List<Budget>(_lstinvestmentCodeBudgets).AsReadOnly(); }
protected set { _lstinvestmentCodeBudgets = value; }
}
public void AddBudget(Budget investment_code_budget) {
if (investment_code_budget != null && !_lstinvestmentCodeBudgets.Contains(investment_code_budget)){
investment_code_budget.oInvestmentCode = this;
_lstinvestmentCodeBudgets.Add(investment_code_budget);
}
}
public override int GetHashCode(){
return (GetType().FullName +"|"+ _investmentCode.GetHashCode()).GetHashCode();
}
}

投资类型

InvestmentType.hbm.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Tec.Core.Model.InvestmentType, Tec.Core" table="tblInvestmentType" lazy="false">
<id name="ID" column="InvestmentTypeID" unsaved-value="0">
<generator class="identity" />
</id>
<property name="InvestmentTypeNote" column="InvestmentTypeNote" />

<bag name="Budgets" table="tblBudget" inverse="true" cascade="save-update">
<key column="InvestmentTypeID" />
<one-to-many class="Tec.Core.Model.Budget, Tec.Core" />
</bag>
</class>
</hibernate-mapping>

InvestmentType.cs

public class InvestmentType: Entity {

    private string _investmentTypeNote="";
private IList<Budget> _lstInvestmentTypeBudgets = new List<Budget>();

private InvestmentType() { }

public InvestmentType(string InvestmentTypeNote){
this._investmentTypeNote = InvestmentTypeNote ;
}
public string InvestmentTypeNote {
get { return _investmentTypeNote ; }
set { _investmentTypeNote = value; }
}
public IList<Budget> Budgets {
get { return new List<Budget>(_lstInvestmentTypeBudgets).AsReadOnly(); }
protected set { _lstInvestmentTypeBudgets = value; }
}
public void AddBudget(Budget investment_type_budget) {
if (investment_type_budget != null && !_lstInvestmentTypeBudgets.Contains(investment_type_budget)){
investment_type_budget.oInvestmentType = this;
_lstInvestmentTypeBudgets.Add(investment_type_budget);
}
}
public override int GetHashCode(){
return (GetType().FullName + "|"+ _investmentTypeNote.GetHashCode()).GetHashCode();
}
}

预算

预算.hbm.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Tec.Core.Model.Budget, Tec.Core" table="tblBudget" lazy="false">
<id name="ID" column="BudgetID" unsaved-value="0">
<generator class="identity" />
</id>
<property name="BudgetAmount" column="BudgetAmount" />
<property name="BudgetYear" column="BudgetYear" />

<many-to-one name="oAirport" column="AirportID"
class="Tec.Core.Model.Airport, Tec.Core" not-null="true" />
<many-to-one name="oInvestmentCode" column="InvestmentCodeID"
class="Tec.Core.Model.InvestmentCode, Tec.Core" not-null="true" />
<many-to-one name="oInvestmentType" column="InvestmentTypeID"
class="Tec.Core.Model.InvestmentType, Tec.Core" not-null="true" />
</class>
</hibernate-mapping>

预算.cs

   public class Budget:Entity<long> 
{
private float _budgetAmount;
private int _budgetYear;
private InvestmentType _investmentType;
private InvestmentCode _investmentCode;
private Airport _airport;

public Budget() { }
public float BudgetAmount {
get{return _budgetAmount; }
set{_budgetAmount = value;}
}
public int BudgetYear {
get{return _budgetYear; }
set{_budgetYear = value;}
}
public Airport oAirport{
get{return _airport; }
set{_airport = value;}
}
public InvestmentType oInvestmentType{
get{return _investmentType; }
set{_investmentType = value;}
}
public InvestmentCode oInvestmentCode {
get{return _investmentCode ; }
set{_investmentCode = value;}
}
public override int GetHashCode(){
return (GetType().FullName + "|" + _airport.GetHashCode() +"|"+ _investmentCode.GetHashCode()+"|"+ _investmentType.GetHashCode()).GetHashCode();
}

}

在我的 Default.asp.cs 中

我有以下代码:

        Airport objA = new Airport("NA", "New Airport")
InvestmentCode objIC = new InvestmentCode("1000", "ABCD");
InvestmentType objIT = new InvestmentType("Capex");

Budget objBg = new Budget();
objBg.oAirport = objA;
objBg.oInvestmentCode = objIC;
objBg.oInvestmentType = objIT;
objBg.BudgetAmount = 10000;
objBg.BudgetYear = 2014;

objA.AddBudget(objBg);
AirportDao.SaveOrUpdate(objA);

objIC.AddBudget(objBg);
daoFactory.GetInvestmentCodeDao().Save(objIC); //Error occur here not-null property references a null or transient value Tec.Core.Model.Budget.oInvestmentType


objIT.AddBudget(objBg);
daoFactory.GetInvestmentTypeDao().Save(objIT);

最佳答案

我不会赌上性命..但我要说你的问题就在这里:

public IList<Budget> Budgets {
get { return new List<Budget>(_lstinvestmentCodeBudgets).AsReadOnly(); }
protected set { _lstinvestmentCodeBudgets = value; }
}

此代码按以下顺序调用:

objA.AddBudget(objBg);
AirportDao.SaveOrUpdate(objA);

objA 被持久化..并且 objBg 被级联并被持久化。这意味着 NHiberante 正在跟踪对 objBg 的引用。

然后你调用这个:

objIC.AddBudget(objBg);

这会按预期将预算添加到列表中。我们将此列表称为“列表 A”。

然后你去调用保存:

daoFactory.GetInvestmentCodeDao().Save(objIC);

当 NHibernate 开始从您的实体中读取值时..它涉及到您的 getter:

get { return new List<Budget>(_lstInvestmentTypeBudgets).AsReadOnly(); }

您已将您的属性(property)包装在一个新列表中。所以现在,这是“列表 B”.. 而不是 NHibernate 期望的“列表 A”。 “列表 B”还没有被持久化……所以 NHibernate 说“等等……你试图持久化一些我没有在更新中跟踪的东西”。

尝试将你的 getter 改成这样:

get { return _lstInvestmentTypeBudgets; }

..看看是否可行。它应该.. 因为你返回的是同一个实例。

关于c# - Nhibernate 非空属性引用空值或 transient 值 Tec.Core.Model.Budget.oInvestmentCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22003511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com