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]