I'm developing a web application for scheduling logistical distribution. I'm using Java, JSF 2.0 and Oracle-XE as database. In Oracle-XE, I have implemented sequences and triggers to auto-increment the ID of some attributes. My problem is, if I execute the application for instance to add a new customer, then I need to retrieve the incremented ID from the database and save it in my customer object afterwards. Is there another way to get the incremented ID from the database开发者_StackOverflow社区 directly? I don't like the solution with SELECTing the ID from the database.
Thanks
If you are issuing SQL directly against the database via JDBC (if you are using an object-relational mapping layer, the API probably supports something similar), you can use the RETURNING clause during an INSERT. Something like
INSERT INTO table_name( column1, column2, ... , columnN )
VALUES( :1, :2, ... , :N )
RETURNING key_column INTO :new_key
That will return the value that the trigger populated for the KEY_COLUMN
column into the :new_key
bind variable.
精彩评论