-->
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: ClassCastException on many-to-many relation persist
PostPosted: Wed Mar 01, 2006 9:02 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
How can I resolve this an persist objects involved in many-to-many relation.

Thx.

Hibernate version:3.1

Mapping documents:

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

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>


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

DocumenteGenerice docGen = new DocumenteGenerice();
docGen.setSerieDoc("DOCGEN00004");
docGen.setNumarDoc("00004");
docGen.setTipDocument("FA");
docGen.setObservatii("DocumentGeneric4");
docGen.setInGarantie(0);

Miscari miscare = new Miscari();
miscare.setObservatii("Miscare4");
miscare.setSensMiscare(1);

docGen.getMiscaris().add(miscare);
miscare.getDocumenteGenerices().add(docGen);

session.persist(miscare);
session.persist(docGen);


Full stack trace of any exception that occurs:

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)


Name and version of the database you are using:MySQL-4.17

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: ClassCastException on many-to-many relation persist
PostPosted: Mon Mar 20, 2006 1:11 pm 
Newbie

Joined: Tue Jul 05, 2005 3:21 pm
Posts: 6
It seems to me that you're trying to model a many-to-many association between two tables.
One table is reflected in the Object Model by the class DocumenteGenerice and the other by the class Miscari.

If this is the case you should have this:
DocumenteGenerice
<set name="miscaris" cascade="save-update" table="miscari_documente" inverse="false">
<key>
<column name="documente_genericeId" length="15" />
</key>
<many-to-many class="lotofsamples.model.Miscari" column="MiscareId"/>
</set>

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.DocumenteGenerice" column="documente_genericeId"/>
</set>

But I suspect that in fact you're trying to model a one-to-many between
DocumenteGenerice and MiscariDocumente on one side and a many-to-one (one-to-many) between MiscariDocumente and Miscari on the other side.
If this is the case then you should use a MiscariDocumente as an intermediate entity class and have two one-to-many associations between three entities. The MiscariDocumente would be the many side of both.

Or use the MiscariDocumente as a value type this time and have a
<set name="miscaris"......
<composite-element class="MiscariDocumente">
<parent ....
<many-to-one name="miscare" class="Miscari" ....
<property.....
</composite-element>
</set>
An example to follow is in CaveatEmptor, mapping between Category and Items

Succes


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.