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
Hibernate version:
3.1
Mapping documents:
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 between sessionFactory.openSession() and session.close():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();
Full stack trace of any exception that occurs: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)
Name and version of the database you are using:mysql 5.0.18
The generated SQL (show_sql=true):Code:
[java] Hibernate: insert into MANUFACTURERS (brandtype, countryoforigin) values (?, ?)
[java] Hibernate: update MODELTYPES set MAKER_ID=? where MODEL_ID=?
Debug level Hibernate log excerpt: