流畅的NHibernate复合键查询

时间:2012-08-22 03:59:37

标签: c# .net fluent-nhibernate

我是Fluent Nhibernate和.NET的新手。如果我的问题不明确,请告诉我。

我在应用程序中有3个表,如下所示

表A

AID(PK),CreatedDate(PK),ZID,AFirstname,ALastname,AAddress,AZipCode

表B

CID(PK),AID(PK),日期(PK),Field1,Field2

表C

CID(PK),Cname

如果我错了,请纠正我。我的模型类如下所示。

public class A {
    public A() {}
    public virtual long AID { get; set; }
    public virtual int ZID { get; set; }
    public virtual DateTime CreatedDate { get; set; }
    public virtual string AFirstName { get; set; }
    public virtual string ALastName { get; set; }
    public virtual string AZip { get; set; }
    public virtual string AAddress { get; set; }
}

Class A的MapperClass

public class AMap : ClassMap<A> {
    public AMap() {
        Table("A");
        CompositeId().KeyProperty(x => x.AID, "AID").KeyProperty(x => x.CreatedDate, "CreatedDate");
        Map(x => x.ZID).Column("ZID").Not.Nullable();
        Map(x => x.AFirstName).Column("AFirstname").Not.Nullable();
        Map(x => x.ALastName).Column("ALastname").Not.Nullable();
        //Same as above for Address and ZipCode
      }
}

My B Model Class如下所示

public class B{
public B() { }
public virtual long AID { get; set; }
public virtual int CID { get; set; }
public virtual DateTime Date { get; set; }
public virtual string Field1 {get; set; }
public virtual string Field2 {get; set; }
}

B类的Mapper类

public class BMap : ClassMap<B> {
    public BMap() {
        Table("B");
        CompositeId().KeyProperty(x => x.AID, "AID").KeyProperty(x => x.CID, "CID").KeyProperty(x => x.Date, "Date");
        Map(x => x.Field1).Column("Field1").Not.Nullable();
        //Same for Field2
    }
}

我的表C模型类如下所示。

public class C{
        public C() { }
        public virtual int C{ get; set; }
        public virtual string Cname{ get; set; }
}

Class C的MapperClass

public class C: ClassMap<C> {
    public C() {
        Table("C");
        Id(x => x.CID).GeneratedBy.Identity().Column("CID");
        Map(x => x.CName).Column("Cname").Length(64);
    }
}

现在很多人可能认为桌子设计不合适。但是在这一点上什么都做不了,这种设计肯定会降低性能,但我想我们必须忍受这一点。

现在我需要一些帮助来编写所有三个表的映射。我也可以使用Criteria API为此编写查询以加入所有3个表。

在我的应用程序中,我从另一个函数获取ZID。现在,根据ZID我需要抓取AFirstName,ALastName,Field1,Field2,CName。怎么可能呢?

请帮忙。如果问题不明确,也请告诉我。

先谢谢。

1 个答案:

答案 0 :(得分:0)

将id替换为对原始属性的引用,以便您可以导航它们

public class B
{
    public virtual A A { get; set; }
    public virtual C C { get; set; }
    public virtual string Field1 {get; set; }
    public virtual string Field2 {get; set; }
}

public class BMap : ClassMap<B>
{
    public BMap()
    {
        Table("B");
        CompositeId()
            .KeyReference(x => x.A, "AID", "Date")
            .KeyReference(x => x.C, "CID");

        Map(x => x.Field1, "Field1").Not.Nullable();
        Map(x => x.Field2, "Field2").Not.Nullable();
    }
}

// query for it

var results = session.CreateCritera<B>()
    .JoinAlias("A", "a")
    .JoinAlias("C", "c")
    .SetProjection(Projections.List()
        .Add(Projections.Property("Field1"), "Field1")
        .Add(Projections.Property("Field2"), "Field2")
        .Add(Projections.Property("a.CreatedDate"), "Date")
        .Add(Projections.Property("c.Name"), "CName"))
    .SetResulttransformer(Transformers.AliasToBean<YourDto>())
    .List<YourDto>();