0
我正在使用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>
我的問題似乎是雙重的。
- 我在我的應用程序模型中的子類UserBooks和UserMagazines實際上是SubTypes的子類型,我不知道如何在我的hbm文件中建模。
- 鑑別器位於LibraryItems表中,但應用於子子類中。