如何将数据库映射到我的模型?

时间:2013-07-13 01:17:21

标签: nhibernate nhibernate-mapping

我正在使用NHibernate 3.3.2.4(不流利)...我正在尝试将数据库模型映射到应用程序模型,我正在解开。

我有以下数据库结构(简化为简洁):

LibraryItems
------------
Id UniqueIdentifier Primary Key,
UserId UniqueIdentifier Foreign Key References Users(UserId),
Type SmallInt,             --Book, Magazine etc.
ProductId UniqueIdentifier --Links to UserBooks When Type = 1
                           --         UserMagazines When Type = 2

UserBooks
---------
ProductId UniqueIdentifier Unique Foreign Key References Books(Id),
--Other fields pertaining to books

UserMagazines
-------------
ProductId UniqueIdentifier Unique Foreign Key References Magazines(Id),
--Other fields pertaining to magazines

UserBookmarks
-------------
Id UniqueIdentifier Primary Key,
LibraryItemId UniqueIdentifier Foreign Key References LibraryItems(Id),
BookmarkLocation NVarChar(100)

我还有以下应用程序模型

public enum LibraryItemType
{
    Book = 1,
    Magazine = 2
}

public abstract class LibraryItem
{
    public Guid Id { get; set; }   //ProductId
    public Guid UserId { get; set; }
    abstract public LibraryItemType Type { get; }
}

public abstract class ReadableLibraryItem : LibraryItem
{
    public Bookmark CurrentPosition { get; set; }
}

public class UserBook : ReadableLibraryItem
{
    public override LibraryItemType Type { get { return LibraryItemType.Book; } }
    //Other properties pertaining to books
}

public class UserMagazine : ReadableLibraryItem
{
    public override LibraryItemType Type { get { return LibraryItemType.Magazine; } }
    //Other properties pertaining to magazines
}

我完全不知道如何从我的数据库中的数据模型映射到应用程序模型...到目前为止,我有:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping default-cascade="save-update" 
                   assembly="Model" 
                   namespace="Model.Library" 
                   default-lazy="false" xmlns="urn:nhibernate-mapping-2.2">

  <class name="LibraryItem" table="LibraryItems" optimistic-lock="version">

    <id name="Id" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="assigned"/>
    </id>

    <discriminator column="Type" type="Int32" />
    <property name="UserId" type="Guid" not-null="true" />
    <property name="ProductId" type="Guid" not-null="true" />
    <property name="Type" type="Model.Library.LibraryItemType, Model" not-null="true" />

    <joined-subclass name="ReadableLibraryItem">

      <one-to-one name="CurrentPosition" type="Model.Library.Bookmark" class="Bookmark" property-ref="LibraryItemId" />

      <joined-subclass name="UserBook" table="UserBooks" lazy="false" discriminator-value="1">
        <key column="Id" />
      </joined-subclass>

      <joined-subclass name="UserMagazineIssue" table="UserMagazineIssues" lazy="false" discriminator-value="2">
        <key column="Id" />
      </joined-subclass>

    </joined-subclass>

  </class>
</hibernate-mapping>

我的问题似乎是双重的。

  1. 我的应用程序模型中的子类型UserBooks和UserMagazines实际上是SubTypes的SubTypes,我不知道如何在我的hbm文件中对其进行建模。
  2. 鉴别器位于LibraryItems表中,但应用于子子类。

1 个答案:

答案 0 :(得分:0)

自从我手动创建映射以来已经有一段时间了,但过去我使用了像这样的映射生成器:http://nmg.codeplex.com/

对NHibernate很重要(就像EF一样)提供从表到类的映射。如果您有子类型,我会将它们视为单独的类,并在映射上添加默认的“where”子句。

因此,每个子类型都有3个映射文件:

  • ReadableLibraryItem
  • UserBook
  • UserMagazine

同样,自从我完成手动映射以来已经有一段时间了,但这就是我要做的事情。

还有一件事,它曾经是所有属性都需要“虚拟”,我不确定是否仍然如此。

希望这有帮助,