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]