-->
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.  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Many-to-Many save problem. pls help.
PostPosted: Wed Mar 01, 2006 6:56 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
In my application I have one-to-many, many-to-one, one-to-one and many-to-many mapping. I can persist all relation objects but not many-to-many ones. I've tried many to many even with 2 one-to-many but without success. Can anybody give me a simple example how can I persist with hibernate objects with many-to-many relation between them.

Thx in advance.

Hibernate version: 3.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Many to Many
PostPosted: Wed Mar 01, 2006 8:00 am 
Newbie

Joined: Sat Oct 29, 2005 1:23 pm
Posts: 15
Location: England
I have had this problem in the past as well, and the only way that I was able to get around it was to create link classes to join the 2 classes together.

eg. User * ---> 1 UserRealm 1 <--- * Realm

I know that this not ideal, as Hibernate should not dictate the domain architecture, but this is the only way I managed to get it to work. The Hibernate book recommends this approach as they argue that link classes may contain additional attributes such as creeatedDate.

I hope that this helps.

_________________
Jamie Cash
eCommerce Consultancy Limited
The eSecurity Experts


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 8:21 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
I've tried this and only one object was saved into db.
In your ex: ses.saveOrUpdate(user);

But: Only in table users hibernate saved values, nothing in tabels realms and user_realms. Can U tell me what I did wrong. If U can, please send me a example code. I can't persist a transaction becose of this.

I wanna persist objects involved in this transaction in any way for a moment...

Thx in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 8:47 am 
Newbie

Joined: Wed Mar 01, 2006 4:14 am
Posts: 16
use inverse="true" cascade="save-update" on both sides

showing the part of your mapping would help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 9:18 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
The problem is that now I've got the following exception:

Exception in thread "main" java.lang.ClassCastException: lotofsamples.model.DocumenteGenerice
at org.hibernate.type.EntityType.toLoggableString(EntityType.java:154)
at org.hibernate.type.CollectionType.toLoggableString(CollectionType.java:146)
at org.hibernate.pretty.Printer.toString(Printer.java:53)
at org.hibernate.pretty.Printer.toString(Printer.java:90)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:97)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at lotofsamples.VerificareChei.main(VerificareChei.java:98)

I think I've tried all combinations of inverse and cascade. But without succes. I don't know what else can do.

Thx.


Top
 Profile  
 
 Post subject: Cascade
PostPosted: Wed Mar 01, 2006 10:44 am 
Newbie

Joined: Sat Oct 29, 2005 1:23 pm
Posts: 15
Location: England
No need to try all combinations. Try cascade = all-delete-orphan and only set inverse=true on the business classes not the link class.

If this does not work, please post your mappings.

_________________
Jamie Cash
eCommerce Consultancy Limited
The eSecurity Experts


Top
 Profile  
 
 Post subject: Re: Cascade
PostPosted: Wed Mar 01, 2006 1:24 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
jcash wrote:
No need to try all combinations. Try cascade = all-delete-orphan and only set inverse=true on the business classes not the link class.
If this does not work, please post your mappings.


[b]Mapping files

DocumenteGenerice
<set name="miscaris" cascade="save-update" table="miscari_documente">
<key>
<column name="documente_genericeId" length="15" />
</key>
<many-to-many class="lotofsamples.model.Miscari" column="MiscareId"/>
</set>

MiscariDocumente

<many-to-one name="miscari" class="lotofsamples.model.Miscari" fetch="select">
<column name="MiscareId" length="17" />
</many-to-one>
<many-to-one name="documenteGenerice" class="lotofsamples.model.DocumenteGenerice" fetch="select">
<column name="documente_genericeId" length="15" />
</many-to-one>

Miscari
<set name="documenteGenerices" table="miscari_documente" inverse="true" cascade="save-update">
<key>
<column name="MiscareId" length="17" />
</key>
<many-to-many class="lotofsamples.model.MiscariDocumente" column="documente_genericeId"/>
</set>



I think something is wrong with MiscariDocumente.hbm.xml. Mapping files were generated by Hibernate Tools, and hibernate tools doesn't generate many-to-many relation.

Thx a lot. [/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 2:02 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
Get rid of the MiscariDocumente.hbm.xml - it is not needed in this example.

The other two mappings look fine.

Then from your java code do the following:
1) create a Miscari object
2) initialize a new Set for Miscari.documenteGenerices
3) create DocumenteGenerice objects... and foreach object you create
make sure to add it to the Miscari.documenteGenerices
5) lastly make sure to add the Miscari object (s) to the DocumenteGenerice.miscari_documente.

Then just run save on your miscari object and it should all persist out to DB. sess.save(miscari);

if you are running into database problems... then I suggest you create your DDL based on the above hbm.xml files and see what hibernate thinks your DDL should look like.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 2:16 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
jt_1000 wrote:
Get rid of the MiscariDocumente.hbm.xml - it is not needed in this example.

The other two mappings look fine.

Then from your java code do the following:
1) create a Miscari object
2) initialize a new Set for Miscari.documenteGenerices
3) create DocumenteGenerice objects... and foreach object you create
make sure to add it to the Miscari.documenteGenerices
5) lastly make sure to add the Miscari object (s) to the DocumenteGenerice.miscari_documente.

Then just run save on your miscari object and it should all persist out to DB. sess.save(miscari);

if you are running into database problems... then I suggest you create your DDL based on the above hbm.xml files and see what hibernate thinks your DDL should look like.


I can't get rid of DocumenteGenerice cose there are others properties that I wanna save into DocumenteGenerice table. And I used an customized increment for that table.

Thx a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 3:06 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I didn't say get rid of DocumenteGenerice - I said: MiscariDocumente
Try my suggestion out to convince yourself that M-M from
Miscari (M) to (M) DocumenteGenerice will work.


fyi...[extra] if you need to store info in the MiscariDocumente table then you'll need to setup a <composite-element> for this - see reference.pdf and search for composite-element to see how to set this up.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 3:36 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
jt_1000 wrote:
I didn't say get rid of DocumenteGenerice - I said: MiscariDocumente
Try my suggestion out to convince yourself that M-M from
Miscari (M) to (M) DocumenteGenerice will work.

fyi...[extra] if you need to store info in the MiscariDocumente table then you'll need to setup a <composite-element> for this - see reference.pdf and search for composite-element to see how to set this up.



I will follow Your advice and i will use composite-element. In previous message I made a mistake. Not DocumenteGenerice but MiscariDocumente is linked table that has info besides foreign keys. In fact foreing keys doesn't compose primary key. Primary key is a custom generator.

Thx a lot for Your advice



Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 3:42 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
TonyXXX -
Again, if you don't need extra info in MiscariDocumente , then the vanilla m-m that I showed you will work - it will take into account all the FK's and intermediate table MiscariDocumente.. (hibernate magic) you won't need to worry about the relationship details... except for defining them in your <set...<many-to-many... tags.

Please rate (Y) if this helps you....thx.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 4:32 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
jt_1000 wrote:
TonyXXX -
Again, if you don't need extra info in MiscariDocumente , then the vanilla m-m that I showed you will work - it will take into account all the FK's and intermediate table MiscariDocumente.. (hibernate magic) you won't need to worry about the relationship details... except for defining them in your <set...<many-to-many... tags.

You're right. I've made a mistake in miscari.hbm.xml. I put
<many-to-many class="lotofsamples.model.MiscariDocumente" column="documente_genericeId"/>
instead of
<many-to-many class="lotofsamples.model.DocumenteGenerice" column="documente_genericeId"/>

I repaired it, I followed Your advice step by step. I ran code without any exception but surprise (at least for me) both log and table checkings show me that hibernate saved one row in two tables(DocumenteGenerice and Miscari) but in linked table (MiscariDocumente) it saved no row. How is that posible? Maybe cose linked table use a surogate pk (generated by a customized generator).

Any idee. Thx in advance.


Please rate (Y) if this helps you....thx.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 4:38 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
can you post your TWO mapping files and the java code (please only a simple test case, not everything in your application) that you are using to save - and I'll work through them with you.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 5:01 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
jt_1000 wrote:
can you post your TWO mapping files and the java code (please only a simple test case, not everything in your application) that you are using to save - and I'll work through them with you.


I've solved it. Thx a lot. The mistake was that I added only in one set and when I added in the other set hibernate saved corect data in all three tables.

Before code was
miscari.getDocumenteGenerices().add(documenteGenerices);
session.persist(miscari);

and working code must be:
miscari.getDocumenteGenerices().add(documenteGenerices);
documenteGenerice.getMiscari().add(miscari);
session.persist(miscari);

But I didn't solve yet the case of saving values in aditional columns of linked table.

Thx a lot man. I will not forget to vote U for helping me.


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

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.