-->
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: does not save many-to-many... hlp
PostPosted: Tue Feb 28, 2006 6:05 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
I hava a many-to-many relation. When I wanna persist all objects involved in this relation only one table is populated with data. I tried a lot, but without a good result.

Hibernate version:3.1

Mapping documents:

DocumenteGenerice.hbm.xml:
<class name="lotofsamples.model.DocumenteGenerice" table="documente_generice" catalog="merp"
dynamic-insert="true" dynamic-update="true">
<id name="documenteGenericeId" type="string">
<column name="documente_genericeId" length="15" />
<generator class="lotofsamples.hibernate.id.HostBasedIdGenerator">
<param name="table">increments</param>
<param name="primary_key_column">nume_tabela</param>
<param name="value_column">val_increment</param>
<param name="primary_key_value">documente_generice</param>
</generator>
</id>

<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>

Miscari.hbm.xml
<class name="lotofsamples.model.Miscari" table="miscari" catalog="merp"
dynamic-insert="true" dynamic-update="true">
<id name="miscareId" type="string">
<column name="MiscareId" length="17" />
<generator class="lotofsamples.hibernate.id.HostBasedIdGenerator">
<param name="table">increments</param>
<param name="primary_key_column">nume_tabela</param>
<param name="value_column">val_increment</param>
<param name="primary_key_value">miscari</param>
</generator>
</id>

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


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

Set setDocGen = new HashSet(1);
DocumenteGenerice docGen = new DocumenteGenerice();
docGen.setSerieDoc("FAC0004");
docGen.setNumarDoc("0004");
docGen.setTipDocument("FA");
docGen.setMasterDoc(1);
docGen.setInGarantie(0);

Set setMiscari = new HashSet(1);
Miscari miscare = new Miscari();
miscare.setObservatii("Miscare1");
miscare.setSensMiscare(1);

docGen.setMiscaris(setMiscari);
miscare.setDocumenteGenerices(setDocGen);

session.saveOrUpdate(docGen);

tx.commit();


Full stack trace of any exception that occurs:No one

Name and version of the database you are using: Mysql-4.1.7

The generated SQL (show_sql=true):

Hibernate: insert into merp.documente_generice (Numar_Doc, Serie_Doc, Tip_Document, master_doc, in_garantie, documente_genericeId) values (?, ?, ?, ?, ?, ?)


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Re: does not save many-to-many... hlp
PostPosted: Tue Feb 28, 2006 7:13 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
is this your intended mapping?

DocumenteGenerice (M)-(M) Miscari
and
Miscari (M) - (M) MiscariDocumente
(issue?)

Regardless, I don't see where you are adding the objects to your sets (miscare to setMiscari and docGen to yoru setDocGen)...

_________________
-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:43 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
I database I have DocumenteGenerice (1) - (n) MiscariDocumente (n) - (1) Miscari. I wanna save one row in every table in the following order: DocumenteGenerice-Miscari-MiscariDocumente. In hibernate mapping I define a many-to-many relation between DocumenteGenerice and Miscari. I wrote these mappings and java code in previous message. Hibernate save data only in DocumenteGenerice table cose I thing I missed something in mapping or java code. Thx a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 4:59 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
You're right. I forgot to add object into Set-s. I did it but now an exception arised. The stacktrace for it is:

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:101)

Now the java code is:
DocumenteGenerice docGen = new DocumenteGenerice();
docGen.setSerieDoc("DOCGEN00004");
docGen.setNumarDoc("00004");
docGen.setTipDocument("FA");
docGen.setObservatii("DocumentGeneric4");
docGen.setInGarantie(0);
Set<DocumenteGenerice> setDocGen = new LinkedHashSet<DocumenteGenerice>(1);
setDocGen.add(docGen);

Miscari miscare = new Miscari();
miscare.setObservatii("Miscare4");
miscare.setSensMiscare(1);
Set<Miscari> setMiscari = new LinkedHashSet<Miscari>(1);
setMiscari.add(miscare);

docGen.setMiscaris(setMiscari);
miscare.setDocumenteGenerices(setDocGen);
session.saveOrUpdate(docGen);

tx.commit();

Thx a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 11:46 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
make sure your mapping is setup to be:
DocumenteGenerice (1) - (n) MiscariDocumente (n) - (1) Miscari
Post that once you have rewritten it. or you can do a m-m with composite-element defining the MiscariDocumente - see page 100 of the reference.pdf for a composite-element example.
http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.pdf

_________________
-JT

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


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.