开发者

NHibernate one-way association with XRef table

I'm stuck trying to get the mapping I want to persist correctly. For my example, I have an ItemY class which can have 0,1,* Assets. However, an Asset can belong to an ItemY or an ItemZ object. I'm trying to use a cross-reference table to store this.

Schema

TABLE [dbo].[ItemY](
    [ItemYID] [int] IDENTITY(1,1) NOT NULL,
    --more columns here

TABLE [dbo].[Asset](
    [AssetID] [uniqueidentifier] NOT NULL,
    [AssetTypeID] [int] NOT NULL,
    [DisplayOrder] [int] NOT NULL,
    [Location] [varchar](255) NOT NULL,

TABLE [dbo].[ItemYAsset](
    [ItemYID] [int] NOT NULL,
    [AssetID] [uniqueidentifier] NOT NULL,

Mappings:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" namespace="My.Namespace">
<class name="ItemY" table="ItemY">
    <id name="ItemYId" column="ItemYID">
        <generator class="identity" />
    </id>
    <bag name="Images" table="ItemYAsset" inverse="true" cascade="all-delete-orphan">
        <key column="ItemYID" not-null="true" />
        <many-to-many class="Asset" column="AssetID" unique="true" />
    </bag>
    <!--more mapping here-->
</class>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" namesp开发者_高级运维ace="My.Namespace">
    <class name="Asset" table="Asset">
        <id name="AssetId" column="AssetID">
            <generator class="guid.comb" />
        </id>
        <property name="DisplayOrder"></property>
        <property name="Location"></property>
    </class>
</hibernate-mapping>

I don't want to expose an ItemY property on Asset, since an Asset doesn't always belong to an ItemY. My ItemY class persists fine, and also persists the Asset class, but there is nothing added to the association table (ItemYAsset). Any idea what I'm doing wrong?


inverse="true" means the "other side" is responsible for persisting the relationship.

Since you have no "other side" (the relationship is unidirectional), remove that attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜