0

我有以下表格:

foo {
    id;
    catColorId;
}

bar {
    fooId;
    catTypeId;
    barName;
}

cat {
     catColorId;
     catTypeId;
     priority;
}

bar表具有复合 IDfooId, catTypeId
cat表具有复合 IDcatColorId, catTypeId

我想将它们映射到:

class Foo {
    int Id;
    IList<Bar> BarList { get; set; }
}

BarList应按 cat.priority 排序

任何想法如何在 .hbm.xml 中映射它?

更新:

如果我能得到映射,那将是完美的,当填写 Foo.BarList 时,会生成查询(或它的一些变体):

select 
    b.fooId, 
    b.catTypeId,
    b.barName,
from
    join foo as f 
    join bar as b on f.id = b.fooId
    join cat as c on f.catColorId = c.catColorId and b.catTypeId = c.catTypeId
where
    f.id = @fooId
order by 
    c.priority

我对最后一次加入有困难:b.catTypeId = c.catTypeId
你如何在 hbm 标记中定义这种限制?

4

1 回答 1

1

您的示例严格来说是一个Many-To-Many映射。

public class Foo {
    public virtual int Id { get; set; }
    public virtual IList<Cat> Cats { get; set; }
}

// there is no Bar entity in this example; it is a cross-reference table used by nhibernate, but there is no entity in your domain

public class Cat {
    public virtual int Id { get; set; }
    public virtual IList<Foo> Foos { get; set; }
}

映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Models"
    namespace="Models">
    <class name="Foo" table="foo">
        <id name="Id" column="Id">
            <generator class="identity" />
        </id>
        <bag name="Cats" table="bar">
            <key column="FooId"></key>
            <many-to-many column="CatId" class="Cat" />
        </bag>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Models"
    namespace="Models">
    <class name="Cat" table="cat">
        <id name="Id" column="Id">
            <generator class="identity" />
        </id>
        <bag name="Foo" table="bar">
            <key column="CatId"></key>
            <many-to-many column="FooId" class="Foos" />
        </bag>
    </class>
</hibernate-mapping>

但是,如果 Foos 和 Cats 之间的关系比关系更有意义(例如,您需要向 Bar 添加其他属性),您将执行以下操作:

public class Foo {
    public virtual int Id { get; set; }
    public virtual IList<Bar> Bars { get; set; }
}

public class Bar
{
    public virtual Foo Foo { get; set; }
    public virtual Cat Cat { get; set; }
}

public class Cat {
    public virtual int Id { get; set; }
    public virtual IList<Bar> Bars { get; set; }
}
// you can get all Cats for a Foo via Foo.Bars and vice versa

映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Models"
    namespace="Models">
    <class name="Foo" table="foo">
        <id name="Id" column="Id">
            <generator class="identity" />
        </id>
        <bag name="Bars" inverse="true">
            <key column="CatId"></key>
            <one-to-many class="Bar"/>
        </bag>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Models"
    namespace="Models">
    <class name="Cat" table="cat">
        <id name="Id" column="Id">
            <generator class="identity" />
        </id>
        <bag name="Bars" inverse="true">
            <key column="FooId"></key>
            <one-to-many class="Bar"/>
        </bag>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Models"
    namespace="Models">
    <class name="Bar" table="bar">
        <composite-id>
          <key-many-to-one class="Foo" name="Foo" column="FooId" />
          <key-many-to-one class="Cat" name="Cat" column="CatId" />
        </composite-id>
    </class>
</hibernate-mapping>

对不起,如果我的 HBM 标记生锈了;我习惯于通过代码进行映射。

于 2012-06-12T07:13:38.863 回答