-->
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.  [ 1 post ] 
Author Message
 Post subject: mapping one to many relationship
PostPosted: Sat Apr 15, 2006 11:13 pm 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hey,
Very basic question, but having problems with it, I have a manufacturer class and a models class with a one to many relationship. What is the correct way to map this, with two classes? The error listed below shows that the model data is not inserted in the model table before the key associating it with the manufacturer class is inserted.

any help would be greatly appreciatad,
thanks in advance,
Illusion

[b]Hibernate version:[/b]
3.1
[b]Mapping documents:[/b]
[code]
set is in manufacturer class, where primary key is MAKER_ID, cars.models class which is the many class, has primary key MODEL_ID

<set name="modelsset" table="manufacturer-model">
<key column="MAKER_ID"/>
<many-to-many column="MODEL_ID"
unique="true"
class="cars.Models"/>
</set>

<set name="modelsset">
<key column="MAKER_ID"
not-null="true"/>
<one-to-many class="cars.Models"/>
</set>
[/code]
[b]Code between sessionFactory.openSession() and session.close():[/b]
[code]
session.beginTransaction();

Carmakers themaker = new Carmakers();
Models newcarmodel = new Models();
newcarmodel.setmodelname("maxima");
newcarmodel.sethorsepower("265");

themaker.setbrandtype(brandtype);
themaker.setcountryoforigin(countryoforigin);
themaker.getmodelsset().add(newcarmodel);

session.save(themaker);

session.getTransaction().commit();

[/code]
[b]Full stack trace of any exception that occurs:[/b]
[code]
[java] Hibernate: insert into MANUFACTURERS (brandtype, countryoforigin) values (?, ?)
[java] Hibernate: update MODELTYPES set MAKER_ID=? where MODEL_ID=?
[java] 22:53:19,052 ERROR AbstractFlushingEventListener:299 - Could not synchronize database state with session
[java] org.hibernate.TransientObjectException: cars.Models
[java] at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
[java] at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
[java] at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:697)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037)
[/code]
[b]Name and version of the database you are using:[/b]
mysql 5.0.18
[b]The generated SQL (show_sql=true):[/b]
[code]
[java] Hibernate: insert into MANUFACTURERS (brandtype, countryoforigin) values (?, ?)
[java] Hibernate: update MODELTYPES set MAKER_ID=? where MODEL_ID=?
[/code]
[b]Debug level Hibernate log excerpt:[/b]


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

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.