在Fluent nHibernate的同一个键上加入多个表
我是nHibernate的新手。 在我的项目中,我试图消除数据库依赖关系,因此使用Fluent nHibernate。 如果没有直接PK-FK关系船,我在加入2个以上的表格时遇到了问题。 以下是我想加入的表格
以下是这些表的类文件
public class RoleMaster
{
public virtual int RoleId { get; set; }
public virtual string RoleName { get; set; }
public virtual string RoleType { get; set; }
public virtual string RoleDesc { get; set; }
public virtual int Status { get; set; }
}
public class UGRoleDetails
{
public virtual int UGRoleID { get; set; }
public virtual ProjectMaster projMaster { get; set; }
public virtual ProcMaster procMaster { get; set; }
public virtual SubProcMaster subProcMaster { get; set; }
public virtual RoleMaster RoleMaster { get; set; }
public virtual UGMaster UGID { get; set; }
public virtual int Active { get; set; }
public override bool Equals(object obj)
{
var other = obj as UGRoleDetails;
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.UGRoleID == other.UGRoleID &&
this.projMaster == other.projMaster &&
this.procMaster == other.procMaster &&
this.subProcMaster == other.subProcMaster &&
this.UGID == other.UGID &&
this.RoleMaster == other.RoleMaster;
}
public override int GetHashCode()
{
unchecked
{
int hash = GetType().GetHashCode();
hash = (hash * 31) ^ UGRoleID.GetHashCode();
hash = (hash * 31) ^ projMaster.GetHashCode();
hash = (hash * 31) ^ procMaster.GetHashCode();
hash = (hash * 31) ^ subProcMaster.GetHashCode();
hash = (hash * 31) ^ UGID.GetHashCode();
hash = (hash * 31) ^ RoleMaster.GetHashCode();
return hash;
}
}
}
public class UserRoleDetails
{
public virtual int UserRoleId { get; set; }
public virtual int UGRoleID { get; set; }
public virtual ProjectMaster ProjMaster { get; set; }
public virtual ProcMaster procMaster { get; set; }
public virtual SubProcMaster subProcMaster { get; set; }
public virtual int UserId { get; set; }
public virtual RoleMaster RoleMaster { get; set; }
public virtual Nullable<int> Status { get; set; }
public override bool Equals(object obj)
{
var other = obj as UserRoleDetails;
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.UserRoleId == other.UserRoleId &&
this.ProjMaster == other.ProjMaster &&
this.procMaster == other.procMaster &&
this.subProcMaster == other.subProcMaster &&
this.UserId == other.UserId &&
this.RoleMaster == other.RoleMaster;
}
public override int GetHashCode()
{
unchecked
{
int hash = GetType().GetHashCode();
hash = (hash * 31) ^ UserRoleId.GetHashCode();
hash = (hash * 31) ^ ProjMaster.GetHashCode();
hash = (hash * 31) ^ procMaster.GetHashCode();
hash = (hash * 31) ^ subProcMaster.GetHashCode();
hash = (hash * 31) ^ UserId.GetHashCode();
hash = (hash * 31) ^ RoleMaster.GetHashCode();
return hash;
}
}
}
这里是我对这些类的流畅映射
public class RoleMasterMappings : ClassMap<RoleMaster>
{
public RoleMasterMappings()
{
Id(x => x.RoleId);
Map(x => x.RoleName, "RoleName");
Map(x => x.RoleType, "RoleType");
Map(x => x.Status, "Status");
Table("RoleMaster");
}
}
public class UGRoleDetailsMap : ClassMap<UGRoleDetails>
{
public UGRoleDetailsMap()
{
CompositeId()
.KeyProperty(x => x.UGRoleID, "UGRoleID")
.KeyReference(x => x.ProjMaster, "ProjId")
.KeyReference(x => x.procMaster, "procId")
.KeyReference(x => x.subProcMaster, "SubProcId")
.KeyReference(x => x.UGID, "UGID")
.KeyReference(x => x.RoleMaster, "RoleId");
Map(x => x.Active, "Active");
References<ProjectMaster>(x => x.ProjId, "ProjId");
References<ProcMaster>(x => x.ProcId, "procId");
References<SubProcMaster>(x => x.SubProcId, "SubProcId");
References<RoleMaster>(x => x.RoleMaster, "RoleId");
References<UGMaster>(x => x.UGID, "UGID");
Table("UGRoleDetails");
}
}
public class UserRoleDetailsMap : ClassMap<UserRoleDetails>
{
public UserRoleDetailsMap()
{
CompositeId()
.KeyProperty(x => x.UserRoleId, "UserRoleId")
.KeyReference(x => x.ProjMaster, "ProjId")
.KeyReference(x => x.procMaster, "procId")
.KeyReference(x => x.subProcMaster, "SubProcId")
.KeyReference(x => x.UserId, "UserId")
.KeyReference(x => x.RoleMaster, "RoleId");
Map(x => x.Status, "Status");
Map(x => x.UserId, "UserId");
References<ProjectMaster>(x => x.ProjMaster, "ProjId");
References<ProcMaster>(x => x.procMaster, "procId");
References<SubProcMaster>(x => x.subProcMaster, "SubProcId");
References<RoleMaster>(x => x.RoleMaster, "RoleId");
Table("UserRoleDetails");
}
}
现在我想为下面的SQL编写Fluent nHibernate查询。 这里UGRoleDetails和UserRoleDetails正在加入过角色ID是用于RoleMaster的主键。 这两张表格没有直接关系。
select UGR.UGId,Usr.UserId from UGRoleDetails UGR inner join UserRoleDetails UsR on UGR.RoleId=UsR.RoleId where usr.Status=1 order by UGR.UGId
任何机构可以帮助我如何运行上述查询。 我试图这样做,但得到错误。 其他四个表格ProjectMaster , ProcMaster , SubProcMaster和UGMaster只是一个带ID-Name对的表格。 没有提到这些人的课程。 提前致谢。
链接地址: http://www.djcxy.com/p/39461.html上一篇: Join multiple table on same key in Fluent nHibernate
下一篇: How to add sort with custom comparer to Fluent nHibernate collection mapping