-->
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: How do you retrieve an auto increment key after persisting t
PostPosted: Fri Nov 21, 2008 8:16 am 
Newbie

Joined: Thu Feb 28, 2008 12:18 pm
Posts: 10
Hi ALL,
I'm using jpa,hibernate on mysql. here Im facing one problem. My tables are keyed with auto_increment primary keys. I need to persist one course object and n branch objects. Code follows ..

Code:
entityManager.persist(course);
for (Branch branch : branchs) {
         if(branch.getTitle()!=null&&!"".equals(branch.getTitle()))
                {
                                branch.setCourse(course);
                                entityManager.persist(branch);
                }
                                 }


But here because of auto_increment id in course table , after persisting
course object is not reflecting it's id. because of this persisting branches failing.
Error is
Code:
13:30:54,812 INFO  [STDOUT] Hibernate:
    insert
    into
        engweb1.course
        (duration, title, id)
    values
        (?, ?, ?)
13:30:54,828 INFO  [STDOUT] Hibernate:
    insert
    into
        engweb1.branch
        (title, Course_id, acro, id)
    values
        (?, ?, ?, ?)
13:30:54,828 WARN  [JDBCExceptionReporter] SQL Error: 1452, SQLState: 23000
13:30:54,828 ERROR [JDBCExceptionReporter]    message from server: "Cannot add or update a child row: a foreign key constraint fails (`engweb1/branch`, CONSTRAINT `fk_Branch_Course` FOREIGN KEY (`Course_id`) REFERENCES `course` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)"
13:30:54,828 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session


please help me

thanks
raghu


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.