Here's the situation:
I have a non-hibernate part of my application that does an insertion in to the RFQ_DATA table using straight SQL:
insert into rfq_data (filename, rfq_data_id, parent_po_id, date_generated, extension, status, type, username) values (?, rfq_data_id_seq.nextval, ?, sysdate, ?, 'PENDING', ?, ?)
As you can see, it sets the key field (rfq_data_id) to rfq_data_id_seq.nextval
I then have a hibernate part of my application that does an insertion into the RFQ_DATA table this way:
Integer lastRfqDataId = (Integer) session.createQuery("select MAX(rd.rfqDataId) from RfqData rd").uniqueResult();
if (lastRfqDataId == null) {
lastRfqDataId = new Integer(0);
}
Integer rfqDataId = new Integer(lastRfqDataId.intValue() + 1);
RfqData rd = new RfqData(rfqDataId, poId, supplierCode, extension, type, username, filename, dResponseDate);
session.save(rd)
As you can see, this code does not reference or update the rfq_data_id_seq, but instead manually finds the max key field and using the that +1 to get the next id to use.
So, besides the obvious synchronization issues that can result from doing it this way (i.e. using two separate statements, one to get the max and one to insert), here’s the main issue I am having.
I get a unique constraint violation whenever the non-hibernate SQL above tries to do an insertion into the rfq_data table whenever that table got previously updated using my hibernate code, since that hibernate code never updated the rfq_data_id_seq sequence.
What’s the easiest and quickest approach for this?
Thank you,
Alex
P.S. (I saw Pietro's custom sequences posting at hibernate.bluemars.net/296.html, and it may or may not be what I need. If it then I need more instructions.)
|