[英]NHibernate - Eager load one-to-many relationship on the basis of type column
我需要急於從同一個表中加載三個一對多關系到其他三個表。 以下是我桌的ERD:
ProcessActionLog是基於ProcessActionType列與ProcessActionEmail , ProcessActionInterviewFeedback和ProcessActionNotesInfo表具有三個一對多關系的表,該表將包含該列的名稱,以下是我對這些表的映射:
1)ProcessActionLog表映射:
class ProcessActionLogMap : ClassMap<ProcessActionLog>
{
public ProcessActionLogMap()
{
//Rest of mappings//
HasMany(x => x.ProcessActionEmail).Cascade.SaveUpdate().Inverse();
HasMany(x => x.ProcessActionInterviewFeedback).Cascade.SaveUpdate().Inverse();
HasMany(x => x.ProcesssActionNotesInfo).Cascade.SaveUpdate().Inverse();
}
2)ProcessActionEmailMapping:
class ProcessActionEmailMap : ClassMap<ProcessActionEmail>
{
public ProcessActionEmailMap()
{
//rest of mappings//
References(x => x.ProcessActionLog, "ProcessActionLogId");
}
}
3) ProcessActionInterview反饋映射:
class ProcessActionInterviewFeedbackMap : ClassMap<ProcessActionInterviewFeedback>
{
public ProcessActionInterviewFeedbackMap()
{
//Rest of mappings//
References(x => x.ProcessActionLog, "ProcessActionLogId");
}
}
4)ProcessActionNotesInfo
class ProcessActionNotesInfoMap : ClassMap<ProcessActionNotesInfo>
{
public ProcessActionNotesInfoMap()
{
//Rest of mappings//
References(x => x.ProcessActionLog, "ProcessActionLogId");
}
}
現在,我嘗試了以下查詢,以渴望加載所有三個關系:
public IList<ProcessActionLog> FetchUserSpecificProcessActionLogs(int UserId)
{
return _session.Query<ProcessActionLog>()
.Where(x => x.MasterUser.Id == UserId)
.Fetch(x => x.ProcessActionEmail)
.Fetch(x => x.ProcessActionInterviewFeedback)
.Fetch(x => x.ProcesssActionNotesInfo)
.ToFuture().ToList<ProcessActionLog>();
}
但這給了我以下錯誤:
Cannot simultaneously fetch multiple bags.
請幫助我解決此問題或提供建議的其他任何方法,讓我可以使用ProcessActionLog Table的processActionType列來加載實體。 謝謝。
最后,通過在映射中使用ISet而不是IList解決了此問題,現在我的代碼如下:
ProcessActionLog實體
public class ProcessActionLog
{
/// <summary>
/// Process Action Emails
/// </summary>
private ISet<ProcessActionEmail> _ProcessActionEmail { get; set; }
/// <summary>
/// Process Action Interview Feedbacks
/// </summary>
private ISet<ProcessActionInterviewFeedback> _ProcessActionInterviewFeedback { get; set; }
/// <summary>
/// Process action notes info
/// </summary>
private ISet<ProcessActionNotesInfo> _ProcessActionNotesInfo { get; set; }
/// <summary>
/// Is process action email
/// </summary>
public virtual bool IsProcessActionEmail
{
get { return ProcessActionType == ProcessActionType.ProcessActionEmail; }
}
/// <summary>
/// Is process action interview feedback
/// </summary>
public virtual bool IsProcessActionInterviewFeedback
{
get { return ProcessActionType == ProcessActionType.ProcessActionInterviewFeedback; }
}
/// <summary>
/// Is process action notes info
/// </summary>
public virtual bool IsProcessActionNotesInfo
{
get { return ProcessActionType == ProcessActionType.ProcessActionNotesInfo; }
}
/// <summary>
/// Process Action Log setter and getter
/// </summary>
public virtual ISet<ProcessActionEmail> ProcessActionEmail
{
get { return (IsProcessActionEmail ? _ProcessActionEmail : null); }
set { _ProcessActionEmail = value; }
}
/// <summary>
/// Process Action Interview Feedback setter and getter
/// </summary>
public virtual ISet<ProcessActionInterviewFeedback> ProcessActionInterviewFeedback
{
get { return (IsProcessActionInterviewFeedback ? _ProcessActionInterviewFeedback : null); }
set { _ProcessActionInterviewFeedback = value; }
}
/// <summary>
/// Process Action Notes info setter and getter
/// </summary>
public virtual ISet<ProcessActionNotesInfo> ProcessActionNotesInfo
{
get { return (IsProcessActionNotesInfo ? _ProcessActionNotesInfo : null); }
set { _ProcessActionNotesInfo = value; }
}
}
ProcessActionEmail實體
public class ProcessActionEmail
{
//Rest of the attributes
/// <summary>
/// Process Action log of this particular email
/// </summary>
public virtual ProcessActionLog ProcessActionLog { get; set; }
ProcessActionInterviewFeedback實體
public class ProcessActionInterviewFeedback
{
/// <summary>
/// Process Action Log
/// </summary>
public virtual ProcessActionLog ProcessActionLog { get; set; }
}
ProcessActionLog映射
class ProcessActionLogMap : ClassMap<ProcessActionLog>
{
public ProcessActionLogMap()
{
Id(x => x.Id);
Map(x => x.LogName).Length(100).Not.Nullable();
Map(x => x.ProcessActionType).CustomType<Int32>().Not.Nullable();
Map(x => x.CreatedAt).CustomType<DateTime>().Not.Nullable();
References(x => x.MasterUser).Column("UserId");
References(x => x.Process).Column("ProcessId");
References(x => x.SystemUser).Column("CreatedBy");
References(x => x.Task).Column("TaskId").Nullable();
HasMany(x => x.ProcessActionEmail).Cascade.SaveUpdate().Inverse();
HasMany(x => x.ProcessActionInterviewFeedback).Cascade.SaveUpdate().Inverse();
HasMany(x => x.ProcessActionNotesInfo).Cascade.SaveUpdate().Inverse();
}
}
並且查詢與問題中的查詢相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.