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 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 Report()
{
ReportTransactions = new List<ReportTransaction>();
ReportExpenses = new List<ReportExpense>();
}*/
public class DateRangeDto

View File

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

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportExpense
{
public virtual int Id { get; set; }
public virtual int ReportId { get; set; }
public virtual int ExpenseId { get; set; }
//public virtual int Id { get; set; }
public virtual Report Report { get; set; }
public virtual Expense Expense { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
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()
{
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()
{
Table("ReportExpenses");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.ExpenseId, "ExpenseId");
CompositeId()
.KeyReference(x => x.Report, "ReportId")
.KeyReference(x => x.Expense, "ExpenseId");
}
}
}

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportTransaction
{
public virtual int Id { get; set; }
public virtual int ReportId { get; set; }
public virtual int TransactionId { get; set; }
//public virtual int Id { get; set; }
public virtual Report Report { get; set; }
public virtual Transaction Transaction { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
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()
{
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()
{
Table("ReportTransactions");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.TransactionId, "TransactionId");
CompositeId()
.KeyReference(x => x.Report, "ReportId")
.KeyReference(x => x.Transaction, "TransactionId");
}
}
}