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

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:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 11:32 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
add cascade="all-delete-orphan" to the set

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 12:12 pm 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
add cascade="all-delete-orphan" to the set

_________________
Please don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.