我遇到了一些非常基本的映射问题,一对多。
类
public class ClaimPerson
{
public virtual decimal PersonID { get; set; }
public virtual decimal? WorkerID { get; set; }
...
public virtual ICollection<InjuryClaim> Claims { get; set; }
}
public class InjuryClaim : EntityBase<InjuryClaim>
{
public virtual decimal ClaimID { get; set; }
public virtual decimal WorkerID { get; set; }
...
public virtual ClaimPerson Worker { get; set; }
}
映射
public class ClaimPersonMap : ClassMap<ClaimPerson>
{
public ClaimPersonMap()
{
Table("CL_CLAIM_PERSON");
Id(x => x.PersonID).Column("PERSON_ID");
Map(x => x.PrimaryFirstName).Column("PRIMARY_FIRST_NAME");
Map(x => x.PrimaryLastName).Column("PRIMARY_LAST_NAME");
HasMany(x => x.Claims).AsBag().KeyColumn("WORKER_ID");
}
}
public class InjuryClaimMap : ClassMap<InjuryClaim>
{
public InjuryClaimMap()
{
Table("CL_INJURY_CLAIM");
Id(x => x.ClaimID).Column("CLAIM_ID");
}
}
查询
var people = session.Query<ClaimPerson>()
.Where(x => x.PrimaryLastName.StartsWith("Strian"))
.Fetch(x => x.Claims)
.ToList();
输出(SQL)
SELECT claimperso0_.PERSON_ID AS PERSON1_0_0_,
claims1_.CLAIM_ID AS CLAIM1_1_1_,
claimperso0_.PRIMARY_FIRST_NAME AS PRIMARY2_0_0_,
claimperso0_.PRIMARY_LAST_NAME AS PRIMARY3_0_0_,
claims1_.WORKER_ID AS WORKER2_0__,
claims1_.CLAIM_ID AS CLAIM1_0__
FROM CL_CLAIM_PERSON claimperso0_
LEFT OUTER JOIN
CL_INJURY_CLAIM claims1_
ON claimperso0_.PERSON_ID = claims1_.WORKER_ID
WHERE claimperso0_.PRIMARY_LAST_NAME LIKE (:p0 || '%');
为什么重复claims1_.CLAIM_ID
列?