poprawki mapowania association tables

This commit is contained in:
Kamil Ryżek 2024-06-08 15:04:52 +02:00
parent 0cd3f18295
commit cdc924243b
6 changed files with 50 additions and 23 deletions

View File

@ -13,10 +13,15 @@ namespace FirmTracker_Server.nHibernate.Reports
public virtual decimal TotalExpenses { get; set; } public virtual decimal TotalExpenses { get; set; }
public virtual decimal TotalBalance { get; set; } public virtual decimal TotalBalance { get; set; }
public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>(); /*public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>();
public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>(); public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>();
public Report()
{
ReportTransactions = new List<ReportTransaction>();
ReportExpenses = new List<ReportExpense>();
}*/
public class DateRangeDto public class DateRangeDto

View File

@ -13,13 +13,15 @@ namespace FirmTracker_Server.nHibernate.Reports
Map(x => x.TotalExpenses); Map(x => x.TotalExpenses);
Map(x => x.TotalBalance); Map(x => x.TotalBalance);
HasMany(x => x.ReportTransactions) /*HasMany(x => x.ReportTransactions)
.Cascade.All() .KeyColumn("ReportId")
.Inverse(); .Cascade.AllDeleteOrphan()
.Inverse();
HasMany(x => x.ReportExpenses) HasMany(x => x.ReportExpenses)
.Cascade.All() .KeyColumn("ReportId")
.Inverse(); .Cascade.AllDeleteOrphan()
.Inverse();*/
} }

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{ {
public class ReportExpense public class ReportExpense
{ {
public virtual int Id { get; set; } //public virtual int Id { get; set; }
public virtual int ReportId { get; set; } public virtual Report Report { get; set; }
public virtual int ExpenseId { get; set; } public virtual Expense Expense { get; set; }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null || GetType() != obj.GetType()) if (obj == null || GetType() != obj.GetType())
{
return false; return false;
}
var other = (ReportExpense)obj; var other = (ReportExpense)obj;
return ReportId == other.ReportId && ExpenseId == other.ExpenseId; return Report != null && Expense != null &&
Report.Id == other.Report.Id &&
Expense.Id == other.Expense.Id;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return HashCode.Combine(ReportId, ExpenseId); unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + (Report?.Id.GetHashCode() ?? 0);
hash = hash * 23 + (Expense?.Id.GetHashCode() ?? 0);
return hash;
}
} }
} }

View File

@ -7,9 +7,9 @@ namespace FirmTracker_Server.nHibernate.Reports
public ReportExpenseMapping() public ReportExpenseMapping()
{ {
Table("ReportExpenses"); Table("ReportExpenses");
Id(x => x.Id); CompositeId()
References(x => x.ReportId, "ReportId"); .KeyReference(x => x.Report, "ReportId")
References(x => x.ExpenseId, "ExpenseId"); .KeyReference(x => x.Expense, "ExpenseId");
} }
} }
} }

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{ {
public class ReportTransaction public class ReportTransaction
{ {
public virtual int Id { get; set; } //public virtual int Id { get; set; }
public virtual int ReportId { get; set; } public virtual Report Report { get; set; }
public virtual int TransactionId { get; set; } public virtual Transaction Transaction { get; set; }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null || GetType() != obj.GetType()) if (obj == null || GetType() != obj.GetType())
{
return false; return false;
}
var other = (ReportTransaction)obj; var other = (ReportTransaction)obj;
return ReportId == other.ReportId && TransactionId == other.TransactionId; return Report != null && Transaction != null &&
Report.Id == other.Report.Id &&
Transaction.Id == other.Transaction.Id;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return HashCode.Combine(ReportId, TransactionId); unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + (Report?.Id.GetHashCode() ?? 0);
hash = hash * 23 + (Transaction?.Id.GetHashCode() ?? 0);
return hash;
}
} }
} }

View File

@ -7,9 +7,9 @@ namespace FirmTracker_Server.nHibernate.Reports
public ReportTransactionMapping() public ReportTransactionMapping()
{ {
Table("ReportTransactions"); Table("ReportTransactions");
Id(x => x.Id); CompositeId()
References(x => x.ReportId, "ReportId"); .KeyReference(x => x.Report, "ReportId")
References(x => x.TransactionId, "TransactionId"); .KeyReference(x => x.Transaction, "TransactionId");
} }
} }
} }