NHibernate 映射文件帮助

发布于 2024-07-19 04:26:41 字数 446 浏览 2 评论 0原文

NHibernate 菜鸟在这里。 寻求有关如何映射以下常见场景的建议:

[Store]
id PK
名称

[库存商品]
id PK
名称

[StockItemStore]
id PK
StockItemId fk
商店 ID fk
ParLevel

我创建了一个域模型,允许使用AssignToStore(Store store) 方法通过StockItem 实体将各种StockItem 分配给各种商店。

我现在使用 nhibernate 来创建我的数据库架构。 如何为这个基本场景设置映射文件?

任何提示都非常感激。

雪佛兰

NHibernate noob here. Looking for advice on how to map the following common scenario:

[Store]
id pk
Name

[StockItem]
id pk
Name

[StockItemStore]
id pk
StockItemId fk
StoreId fk
ParLevel

I have created a domainmodel that allows various StockItems to be assigned to various Stores via the StockItem Entity using a AssignToStore(Store store) method.

I am now using nhibernate to create my db schema. How do I setup the mapping files for this basic scenario?

Any tips greatly appreciated.

Chev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-07-26 04:26:41

不幸的是,这种关系并不是在 nHibernate 中建模最简单的事情,并且在尝试对链接表中的数据进行查询时,您会遇到一些固有的问题,这将需要一些复杂的解决方法,但是一旦您设置好了它效果很好。

我的方法是将其设置为两个多对一映射,在 Store 映射中具有以下关系,在 StockItem 映射中具有逆关系。

<bag name="StockItems" table="StockItemStore" lazy="true">
  <key column="StoreId" />
  <composite-element class="SuperStore.Components.StockItemStore, SuperStore.Components">
    <property name="ParLevel" type="Int32" />
    <many-to-one name="StockItem" column="StockItemId" class="SuperStore.Components.StockItem, SuperStore.Components" fetch="join" cascade="all" />
  </composite-element>
</bag>

Store 类将具有以下集合:


公共虚拟IList< 库存商品商店> StockItems {获取;设置;}

和 StockItem 将再次出现相反的情况:


公共虚拟IList< 库存商品商店> 存储 {get;set;}
StockItemStore

对象能够包含任一对象(Store 或 StockItem)以及链接表中的任何附加信息。 (在这种情况下,只有 ParLevel。

取决于您从 Store 或 StockItem 中查看 StockItemStore 对象的哪一侧将为空。它可以分为两个类,但我发现这种方法更容易使用。它只需要您作为开发人员知道您从哪一侧接近它,但在我看来,这是使代码更简单且更可重用的一个很好的权衡,

public class StockItemStore
{
    private StockItem stockItem;
    private Store store;

    public virtual StockItem StockItem
    {
        get
        {
            if (stockItem == null)
            {
                stockItem = new StockItem();
            }

            return stockItem;
        }
        set
        {
            stockItem = value;
        }
    }

    public virtual Store store
    {
        get
        {
            if (store == null)
            {
                store = new Store();
            }

            return store;
        }
        set
        {
            store = value;
        }
    }

    public virtual int ParLevel { get; set; }

}

我的方法不使用您在问题中定义的 StockItemStore 中的单个唯一标识符,但是但它在过去总体上对我很有帮助,如果您确实需要的话,

这种方法非常适合使用 HQL。你正在尝试使用 ICriteria 查询,它往往会有点奇怪,有一些使用数据库视图来模拟另一个表用于查询的解决方案,

如果你需要执行 ICriteria 查询,请告诉我。我可以发布一些示例代码。

  • 最大限度

Unfortunately this relationship isn't the easiest thing to model in nHibernate and there are some inherent problems you will encounter when trying to do queries on the data in the linking table, that will require some complicated workarounds, but once you get it set up it works quite well.

My approach to this is to set it up as two many-to-one mappings with the following relationship in the Store mapping and the inverse relationship in the StockItem mapping.

<bag name="StockItems" table="StockItemStore" lazy="true">
  <key column="StoreId" />
  <composite-element class="SuperStore.Components.StockItemStore, SuperStore.Components">
    <property name="ParLevel" type="Int32" />
    <many-to-one name="StockItem" column="StockItemId" class="SuperStore.Components.StockItem, SuperStore.Components" fetch="join" cascade="all" />
  </composite-element>
</bag>

the Store class will have the following collection:


public virtual IList< StockItemStore > StockItems {get;set;}

and StockItem will have the inverse again:


public virtual IList< StockItemStore > Stores {get;set;}

The StockItemStore object is able to contain either object (Store or StockItem) and any additional information that is in the linking table. (in this case just the ParLevel.

depending on which side you are looking at the StockItemStore object from either Store or StockItem will null. It could be broken out into two classes, but I find this approach easier to work with. It just requires you as the developer to know which side you are approaching it from, but its a good tradeoff for making the code simpler and more reusable in my opinion

public class StockItemStore
{
    private StockItem stockItem;
    private Store store;

    public virtual StockItem StockItem
    {
        get
        {
            if (stockItem == null)
            {
                stockItem = new StockItem();
            }

            return stockItem;
        }
        set
        {
            stockItem = value;
        }
    }

    public virtual Store store
    {
        get
        {
            if (store == null)
            {
                store = new Store();
            }

            return store;
        }
        set
        {
            store = value;
        }
    }

    public virtual int ParLevel { get; set; }

}

My approach doesn't use the single unique identifier in the StockItemStore as you defined in your question, but rather what amounts to a composite key in the linking table. But it has served me well in the past overall. I'm sure you could shoehorn that id in somehow if you really needed it.

This approach works great for querying using HQL. if you are trying to use ICriteria queries it tends to get a little wonky. There are some solutions out there with using database views to simulate another table to use for querying that have worked for me.

If you need to do ICriteria queries let me know and I can post some sample code.

  • Max
潇烟暮雨 2024-07-26 04:26:41

我强烈建议阅读 nHibernate。 这是一个非常好的起点:

nHibernate 常见问题解答

我还建议您在前几次手动进行映射。 之后,您应该查看 Fluent nHibernate。 Fluent nHibernate 可以(除其他外)自动从您的域模型生成映射,并帮助您生成数据库模式。 它是一个非常灵活的工具,而且越来越好。 您可以在这里找到它:

Fluent nHibernate

祝你好运!

I would strongly recommend reading up on nHibernate. Here's a very good starting point:

The nHibernate FAQ

I would also recommend that you do the mappings by hand the first couple of times. After that, you should check out Fluent nHibernate. Fluent nHibernate can (among other things) automatically generate the mappings for you from your domain model and also help you to generate the database schema. It is a very flexible tool that is getting better and better. You'll find it here:

Fluent nHibernate

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文