-->
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: why does hibernate always do an insert first?
PostPosted: Wed Mar 30, 2005 1:25 am 
Beginner
Beginner

Joined: Mon Nov 15, 2004 8:32 pm
Posts: 36
Hi,
We have the following code to do an delete then following by an saveOrUpdate. The purpose is to delete an existing copy from db when a new transient object is saved. However, the sql generated by hibernate always did an insert first, so we got a duplicate key exception when we tried to persistent a transient instance whose existing row was not deleted first as we had expected.
I understand there are reasons Hibernate reorders the sqls. However, I would expect it could be able to handle this case. Is there any way we can force hibernate to do the sql delete first?



session = getSession();
transaction = session.beginTransaction();
if (object == null) {
session.delete(deleteQueryString, values, types);
} else {
if (isTransient(session, object)) {
session.delete(deleteQueryString, values, types);
System.out.println("delete called");
}
session.saveOrUpdate(object);
}

transaction.commit();

Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:[/b]
2.1.7c

[b]Mapping documents:[/b]
N/A

[b]Code between sessionFactory.openSession() and session.close():[/b]
See above

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:[/b]
Postgres 7.2.3

[b]The generated SQL (show_sql=true):[/b]

Hibernate: select objectinst0_.object_id as object_id, objectinst0_.object_name as object_n2_, objectinst0_.class_name as class_name, objectinst0_.time_stamp as time_stamp, objectinst0_.sequence_number as sequence5_ from hd_object_instance objectinst0_ where (objectinst0_.object_name=? )
delete called
Hibernate: select nextval ('seq_hd_object_id')
Hibernate: insert into hd_object_instance (object_name, class_name, time_stamp, sequence_number, object_id) values (?, ?, ?, ?, ?)

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 4:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
call flush after delete

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Editing collections
PostPosted: Tue Apr 12, 2005 5:40 pm 
Newbie

Joined: Fri Feb 18, 2005 8:06 am
Posts: 4
Hi,
when I intent save a instance with a collection updated (insert and delete elements), the sql generated is "insert/update" and then "delete". ¿Is possible configure Hibernate for perform a "delete" and then "insert/update"?

Thanks,

Hernán.-


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.