Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Creating new Entries with SQLContainer
Hello everyone,
I tried to persist new data with the SQLContainer and did it like this:
DBService dbService = new DBService();
SQLContainer containterAcc = dbService.getContainerAcc();
Object itemId = containterAcc.addItem();
containterAcc.commit();
The problem here is that the ID of the table stays NULL and since there is a primary key constraint the action fails. I thought that the sequence would count the ID as needed, but unfortunately it is not triggered. How can I do this? Unfortunately I couldn't find something about this in the documentation..
Additional information: I need to trigger the sequence somehow which should generate a new ID for the table.
Hi,
you might want to extend the TableQuery class (implements QueryDelegate) and give it as a parameter to the SQLContainer you when you create it. The method storeRow is where the save or update happens.
Hope this helps,
Olli
Hi,
thank you for the tip. I solved it by building a trigger for the table like this:
CREATE OR REPLACE TRIGGER trigger
BEFORE INSERT ON TABLENAME
FOR EACH ROW
BEGIN
SELECT SEQUENCE.NEXTVAL
INTO :new.PRIMARY_KEY
FROM dual;
END;
That should work, as well as using an autoincrement field, if your database supports it.
-Olli
Olli Tietäväinen: That should work, as well as using an autoincrement field, if your database supports it.
-Olli
Yes, but unfortuantely we run a Oracle 11g Database which doesn't support autoincrement fields. So this is a perfect solution for the problem I had.