I have a problem that occurs sometimes. I have not been able to identify any differences in the situation that causes the problem and the situation where everything works.
I am creating a new object. When I review the SQL log for postgresql in the working situation I can see:
Code:
2005-08-03 17:00:53 LOG: statement: select x.linker_id as linker_id, x.subclass as subclass, x.organization_name as organiza5_, x.created as created, x.modified as modified from linkers x where x.subclass in ('COMPANY', 'CLIENT_COMPANY') and ((x.organization_name='Company X' ))
2005-08-03 17:00:53 LOG: duration: 3.942 ms
2005-08-03 17:00:53 LOG: statement: select nextval ('hibernate_sequence')
2005-08-03 17:00:53 LOG: duration: 1.878 ms
2005-08-03 17:00:57 LOG: statement: insert into linkers (organization_name, created, modified, subclass, linker_id) values ('Company X', '2005-08-03 17:00:53.652000-07', '2005-08-03 17:00:53.652000-07', 'COMPANY', 12746)
2005-08-03 17:00:57 LOG: duration: 17.492 ms
2005-08-03 17:00:57 LOG: statement: update linkers set organization_name='Company X', created='2005-08-03 17:00:53.652000-07', modified='2005-08-03 17:00:56.706000-07' where linker_id=12746
2005-08-03 17:00:58 LOG: statement: insert into links (resume_id, cover_letter_id, stars, distance, created, modified, source_id, target_id, current_status, subclass, link_id) values (null, 12744, null, null, '2005-08-03 17:00:49.507000-07', '2005-08-03 17:00:49.507000-07', 12741, 1431, 'sent', 'APPLICATION', 12745)
(I do not understand why there is the second, seemingly repetitive update statement.) When the problem occurs however the SQL looks different, like this:
Code:
2005-08-03 17:42:52 LOG: statement: select x.linker_id as linker_id, x.subclass as subclass, x.organization_name as organiza5_, x.created as created, x.modified as modified from linkers x where x.subclass in ('COMPANY', 'CLIENT_COMPANY') and ((x.organization_name='Company X' ))
2005-08-03 17:42:52 LOG: duration: 1.748 ms
2005-08-03 17:42:52 LOG: statement: select nextval ('hibernate_sequence')
2005-08-03 17:42:52 LOG: duration: 0.759 ms
2005-08-03 17:42:53 LOG: statement: insert into links (position_title, position_description, start_date, end_date, current, created, modified, source_id, target_id, current_status, subclass, link_id) values ('CEO', null, null, null, '0', '2005-08-03 17:42:52.427000-07', '2005-08-03 17:42:52.427000-07', 1, 6, null, 'MANAGEMENT', 7)
2005-08-03 17:42:53 LOG: statement: SELECT 1 FROM ONLY "public"."linkers" x WHERE "linker_id" = $1 FOR UPDATE OF x
2005-08-03 17:42:53 LOG: statement: SELECT 1 FROM ONLY "public"."linkers" x WHERE "linker_id" = $1 FOR UPDATE OF x
2005-08-03 17:42:53 ERROR: insert or update on table "links" violates foreign key constraint "fk6234fb9cf634a89"
DETAIL: Key (target_id)=(6) is not present in table "linkers".
In the second situation (where the operation fails) hibernate seems to SKIP the insert statement. It is selecting next value from the database and moves forward with the second insert as though the first item was properly saved, but the sql INSERT statement is never issued to the database.
Like I said, sometimes this problem doesnt happen, sometimes it does.
Thanks
Hibernate version: 2.1
Name and version of the database you are using: PostgreSQL 7.1 OS X