I am forced to manage an auto-increment field from code. I've decided to write a stored procedure to do this for me. The idea is to have a procedure that inserts a new row and returns the auto-incremented value to Java so I can work with it further. The following is what I have so far. I don't know what to change to fill the gaps to make it all work.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE addNewMSO
@sourceApplication char(8),
@selectionStatusDate datetime = NULL,
@sysLstUpdtUserId char(10)
AS
BEGIN
SET NOCOUNT ON;
Declare @newVal int
SET @newVal = (select max(seqNo) from MemberSelectedOptions) + 1
INSERT INTO MemberSelectedOptions
([SourceApplication]
,[SeqNo]
,[SelectionStatusDate]
,[SysLstUpdtUserId])
VALUES
(@sourceApplication
,@newVal
,@selectionStatusDate
,@sysLstUpdtUserId)
END
GO
Then in my Java code I have the following.
@NamedNativeQuery(name="addNewMSO",
query="exec addNewMSO :sourceApplication :selectionStatusDate :sysLstUpdtUserId", callable=true)
And my DAO calls it like this...
Query q = session.getNamedQuery("addNewMSO");
q.setParameter("sourceApplication", mso.getSourceApplication());
q.setParameter("selectionStatusDate", mso.getSelectionStatusDate());
q.setParameter("sysLstUpdtUserId", mso.getSysLstUpdtUserId());
q.executeUpdate();
Even then, this doesn't work, cause I get a "Pure native scalar queries are not yet supported" error. So I have no idea where to go next. I've read through the Hibernate docs, but I don't know how to get a return value from the SP. But I still can't even call it...ugh!
TIA for all help.