-->
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.  [ 5 posts ] 
Author Message
 Post subject: help with many to many
PostPosted: Mon Jan 23, 2006 9:58 pm 
Newbie

Joined: Wed Dec 28, 2005 4:54 pm
Posts: 10
I everybody:
I'm developing an app using Oracle 10g as DataBase Manager.
I have a many to many ralationship between a Document table and Move table.

I use the following mapping to each table(Document, Move and Document-Move)

The Document:

<class name="WindowsApplication.Document.Doc, WindowsApplication.Document" table="Documents">
<id name="Id" type="Int32">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_DOCS</param>
</generator>
</id>
<set name="DocMoves" inverse="true" lazy="true">
<key>
<column name="IDDoc" not-null="true" />
</key>
<one-to-many class="WindowsApplication.Document.DocMove, WindowsApplication.Document" />
</set>
</class>

The Move:

<class name="WindowsApplication.Document.Move, WindowsApplication.Document" table="Moves">
<id name="Id" type="Int32">
<column name="ID" />
<generator class="sequence">
<param name="sequence">SEQ_Moves</param>
</generator>
</id>
<set name="DocMoves" inverse="true" lazy="true">
<key>
<column name="IDMove" not-null="true" />
</key>
<one-to-many class="WindowsApplication.Document.DocMove, WindowsApplication.Document" />
</set>
</class>

The Document-Move table:
<class name="WindowsApplication.Document.DocMove, WindowsApplication.Document" table="Document-Move" >
<composite-id name="Id" class="WindowsApplication.Document.DocMoveId, WindowsApplication.Document">

<key-property name="IdDoc" type="Int32">
<column name="IDDoc" />
</key-property>

<key-property name="IdMove" type="Int32">
<column name="IDMove" />
</key-property>

</composite-id>

<many-to-one name="Move" class="WindowsApplication.Document.Move, WindowsApplication.Document" update="false" insert="false" fetch="select">
<column name="IDMove" not-null="true" />
</many-to-one>

<many-to-one name="Doc" class="WindowsApplication.Document.Document, WindowsApplication.Document" update="false" insert="false" fetch="select">
<column name="IDDoc" not-null="true" />
</many-to-one>
</class>

The problem appears when I try to insert a new Doc, my app then generates a new Move, of course I have to insert a DocMove relation. But that is exactly what doesn't occur. I have tried everything, I even create a DocMove object and I assign the values but still doesn't work. In the database appears the Doc, the Move but the relation Document-Move is missing.
Any idea??? Are my mappings ok???

Hel please!!!!1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 10:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Looks like strangeness with inverse="true". The way you've set up your mapping, Document-Move is the "master", but won't create a Doc and a Move because you have insert/update = false. It's probable that you want to turn inverse="true" off for the DocMoves set in Document. That makes Document the "master": so long as you save the Move before the Document, the Document-Move will be created for you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 10:39 pm 
Newbie

Joined: Wed Dec 28, 2005 4:54 pm
Posts: 10
tenwit wrote:
Looks like strangeness with inverse="true". The way you've set up your mapping, Document-Move is the "master", but won't create a Doc and a Move because you have insert/update = false. It's probable that you want to turn inverse="true" off for the DocMoves set in Document. That makes Document the "master": so long as you save the Move before the Document, the Document-Move will be created for you.


The reason I put the insert/update in false is because when I removed it I got an NHibernate error. Could you please explain me more about the inverse="true" . The way I see it the Master in the relation is the Doc. What changes should I do??? Is the composite-id ok???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 5:22 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Hi,

First of all what you did is a many-to-one-to-many mapping not many to many. If you are not expecting the link table to be added more attributes than use something like this:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="..Document" >
<id ... />

<set name="MoveSet" table="Document-Move" cascade="save-update" lazy="true" inverse="false">
<key column="IdDocument" />
<many-to-many class="...Move" column="IdMove" />
</set>
....
</class>
</hibernate-mapping>

When mapping the Move class the process is similar, just that the other set will be inverse=false.

I really don't recommend you using bag instead of set as the collection will be recreated at each update. If you really want the mapping to by many-one-many please reply and I will try to find you a solution. For this cases I always have an id for the link class as-well (any class has a simple id from my perspective) so my experience with composite id's is not notable..

Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 5:27 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Oh, and by the way:

Setting inverse=true on a collection makes that collection unsensitive from updates from NHibernate point of view.

This is recommended approach because otherwise an insert in a collection would translate in an insert with null fk and an update on the fk (I may be mistaken though).

Anyway, this only affects wich association end handles updates/inserts but cascades are another thing, transitive persistence is still available (by this meaning the adding a transitive instance to a persistent collection that has at least cascade=save-update will imply that the transitive instance will become persistent too, and because inverse=true, this instance will be the one that will dictate the save).

Hope it helps,
Dragos


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.