These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Bidirectional mapping question
PostPosted: Mon Mar 10, 2008 10:31 am 
Newbie

Joined: Tue Feb 26, 2008 10:42 am
Posts: 5
Location: England
Hello there I hope someone can help me with my mapping problem. I have two entities that both have relationships to eachother. The entities looks as so:

Code:
public class Asset : EntityBase
{
    private long _id;
    private AssetVersion _currentVersion;
    private IList<AssetVersion> _allVersions;
}

public class AssetVersion
{
    private long _id;
    private Asset _asset;
    private int _versionNumber;
}



The mapping files look as so:

Code:
<class name="...Asset" table="Asset">
    <id name="Id" column="Id" type="Int64" unsaved-value="0">
        <generator class="native"/>
    </id>

    <many-to-one name="CurrentVersion" class="...AssetVersion" column="CurrentVersion"/>

    <bag name="AllVersions" inverse="true" order-by="Version ASC">
        <key column="AssetId"/>
        <one-to-many class="...Asset"/>
    </bag>
</class>

<class name="...AssetVersion" table="AssetVersion">
    <id name="Id" column="Id" type="Int64" unsaved-value="0">
        <generator class="native"/>
    </id>

    <many-to-one name="Asset" class="...Asset" column="AssetId"/>

    <property name="VersionNumber" column="VersionNumber" type="Int32" not-null="true"/>
</class>



As you can see, the Asset class has a reference to a single AssetVersion entity, and also to a collection of AssetVersion entities. However when I try to assign a (persisted) AssetVersion to the Asset.CurrentVersion field, NHibernate falls over with this message:

Code:
Unable to evaluate the expression. Operation not supported. Unknown error: 0x8004f0ef.


It is not an exception that i can see - when this happens the program closes and i am even chucked out of the debugger!


I have read in the book (Beginning Hibernate: From Novice To Professional - Apress), that it is poor design to use bidirectional mapping. In this case, is there a better way to achieve what i am looking for? I appreciate any help anyone can give me!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 3:58 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
<bag name="AllVersions" inverse="true" order-by="Version ASC">
<key column="AssetId"/>
<one-to-many class="...Asset"/>
</bag>

The one-to-many should point to a AssetVersion not an Asset, is that a typo ? Besides that, I think, either the many-to-one relation to CurrentVersion should be a one-to-one or the one-to-many relation to AllVersions should be a many-to-many.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.