Hi All,

i'm quiet new to Nhibernate and i'm running into problems trying to
map a many-to-many bidirectional association. I've taken the advice of
the authors of NHibernate In Action  (which is prob my main ref guide
at the moment)  and attempted to map this as 2  many-to-one
associations but it's simply not working for me?  Help would be
greatly appreciated .... so here goes ... the model:

My tables are  Asset  ..   AssetBinderReference  ..   Binder.

Binder has many Assets and  Assets can belong to many Binders.

AssetBinder  is mapped as an entity in it's own right  .. .it so
happens that as per the authors of Nhib in Action, my association
entity has other properties it needs to store anyway.

So the mapping files:

ASSET:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > <!--
namespace="Data.NHibernateMaps" assembly="xxxx.Data">-->
        <class name="xxxx.Domain.Asset, xxxxx.Domain" table="dbo.Asset"
lazy="true">
                <id name="Id" column="AssetID">
                        <generator class="native" />
                </id>
                <property name="Identifier" column="Identifier" />
                <property name="StartDate" column="StartDate" />
                <property name="ExpiryDate" column="ExpiryDate" />
                <property name="CreateDate" column="CreateDate" />
                <property name="IsCheckedOutInd" column="IsCheckedOutInd" />
                <property name="IsCheckedOutTo" column="IsCheckedOutTo" />
                <property name="LegacyDocID" column="LegacyDocID" />
                <many-to-one name="AssetType" column="AssetTypeID"
class="xxxx.Domain.AssetType, xxxx.Domain"/>
                <bag name="AssetBinderReferences" lazy="true" 
cascade="all-delete-
orphan" inverse="true" >
                        <key column="AssetID"></key>
                        <one-to-many class="xxxx.Domain.AssetBinderReference,
xxxx.Domain"></one-to-many>
                </bag>
                <bag name="AssetPropertyValues" lazy="true" cascade="all-delete-
orphan" inverse="true">
                        <key column="AssetID"></key>
                        <one-to-many class="xxxx.Domain.AssetPropertyValue, 
xxxx.Domain"></
one-to-many>
                </bag>
                <bag name="Tags" table="dbo.AssetTag" lazy="true" cascade="all"
inverse="false" >
                        <key column="AssetID"></key>
                        <many-to-many column="TagID" class="xxxx.Domain.Tag, 
xxxx.Domain" /
>
        </bag>
        </class>
</hibernate-mapping>

ASSETBINDERREFERENCE:


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > <!--
namespace="xxxx.Data.NHibernateMaps" assembly="xxxx.Data">-->
        <class name="xxxx.Domain.AssetBinderReference, xxxxx.Domain"
table="dbo.AssetBinderReference" lazy="true">
                <id name="Id" column="AssetBinderRefID" unsaved-value="0" >
                        <generator class="native"  />
                </id>
                <property name="Rank" column="Rank" />
                <many-to-one name="Asset" column="AssetID" 
class="xxxx.Domain.Asset,
xxxx.Domain" fetch="join" />
                <many-to-one name="Binder" column="BinderID"
class="xxxx.Domain.Binder, xxx.Domain" />
                <many-to-one name="Platform" column="PlatformID"
class="xxxx.Domain.Platform, xxxx.Domain" />
        </class>
</hibernate-mapping>

BINDER:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > <!--
namespace="xxxx.Data.NHibernateMaps" assembly="xxxx.Data">-->
        <class name="xxxx.Domain.Binder, xxxxx.Domain" table="dbo.Binder"
lazy="true">
                <id name="Id" column="BinderID">
                        <generator class="native" />
                </id>
                <property name="Title" column="Title" />
                <property name="SourceBinderID" column="SourceBinderID" />
                <property name="ActivationDate" column="ActivationDate" />
                <property name="CreateDate" column="CreateDate" />
                <property name="ExpiryDate" column="ExpiryDate" />
                <property name="IsCheckedOutInd" column="IsCheckedOutInd" />
                <property name="IsProtectedInd" column="IsProtectedInd" />
                <property name="IsDeletedInd" column="IsDeletedInd" />
                <property name="BinderDate" column="BinderDate" />
                <property name="AllowAds" column="AllowAds" />
                <many-to-one name="BinderStatus" column="BinderStatusID"
class="xxxxx.Domain.BinderStatus, xxxxx.Domain" />
                <many-to-one name="BinderType" column="BinderTypeID"
class="xxxx.Domain.BinderType, xxxx.Domain" />
                <many-to-one name="User" column="UserID" 
class="xxxxx.Domain.User,
xxxxx.Domain" />
                <bag name="AssetBinderReferences" lazy="true" 
cascade="all-delete-
orphan" inverse="true" >
                        <key column="BinderID"></key>
                        <one-to-many class="xxxxx.Domain.AssetBinderReference,
xxxxx.Domain"></one-to-many>
                </bag>
                <bag name="IndexRankSets" lazy="true" 
cascade="all-delete-orphan"
inverse="true" >
                        <key column="BinderID"></key>
                        <one-to-many class="xxxxx.Domain.IndexRankSet, 
xxxxx.Domain"></one-
to-many>
                </bag>
                <bag name="RelatedBinders1" lazy="true" cascade="all" 
inverse="true"
>
                        <key column="ParentBinderID"></key>
                        <one-to-many class="xxxxx.Domain.RelatedBinder, 
xxxxx.Domain"></one-
to-many>
                </bag>
                <bag name="RelatedBinders2" lazy="true" cascade="all" 
inverse="true"
>
                        <key column="ChildBinderID"></key>
                        <one-to-many class="xxxxx.Domain.RelatedBinder, 
xxxxx.Domain"></one-
to-many>
                </bag>
                <bag name="Tags" table="dbo.BinderTag" lazy="true" cascade="all"
inverse="false" >
                        <key column="BinderID"></key>
                        <many-to-many column="TagID" class="xxxxxx.Domain.Tag,
xxxx.Domain" />
        </bag>
        </class>
</hibernate-mapping>


My bit of test code to attempt to save a Binder  :



            var asset = new Asset {Identifier = "NamaImage",
CreateDate = DateTime.Now, AssetType = imageAsset};
            var binder = new Binder {Title = "NewTestBinder",
CreateDate = DateTime.Now};


            var assetBinderReference = new AssetBinderReference {Asset
= asset, Binder = binder, Rank = 1};

            asset.AssetBinderReferences.Add(assetBinderReference);
            binder.AssetBinderReferences.Add(assetBinderReference);

            assetRepository.SaveOrUpdate(asset);
            binderRepository.SaveOrUpdate(binder);


And the Output  which you'll see is simply doing nothing ?

NHibernate: select top 1 assettype0_.AssetTypeID as AssetTyp1_14_,
assettype0_.Name as Name14_, assettype0_.AssetTypeGroup as
AssetTyp3_14_ from dbo.AssetType assettype0_ where
assettype0_.na...@p0;@p0 = 'Image'
NHibernate: SELECT this_.AssetTypeID as AssetTyp1_14_0_, this_.Name as
Name14_0_, this_.AssetTypeGroup as AssetTyp3_14_0_ FROM dbo.AssetType
this_ WHERE this_.Name = @p0;@p0 = 'Image'

System.NullReferenceException: Object reference not set to an instance
of an object.
at
Tests.xxxx.Data.RepositoryTests.BinderRepositoryTests.CanCreateBinder
() in BinderRepositoryTests.cs: line 106

Again,  advice greatly appreciated!

Thanks

Griff

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to