New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
INSERT/UPDATE ... RETURNING #971
Comments
any thoughs on this? |
@brunomanzo Contributions are welcome. I haven't had time to look into this. |
@timowest After some tests I did the following:
5 - Get the entity that will be updated and find the name of the id field (sadly here not all tables have a "id" field, sometimes its "id_tableName"). In my case the id field was enough. I will open a PR later with the code so you can take a look, but what you think about the idea? |
|
@Shredder121 I see, but for my case the application is built in top of hibernate, so i had to do that work around, it's working fine for me. But makes sense what you said :) |
Yes and I'm not saying that workarounds shouldn't be there (it's a risk of the trade). |
I agree with @Shredder121, as a Querydsl contribution we would accept it only in the scope of Querydsl SQL. |
In QueryDSL SQL with PostgreSQL an insert with a call to I guess this is because the only way to get the generated primary key with PostgreSQL is by adding the 'returning MY_ID' at the end of the insert clause. Do you have a workaround for this? Or a pointer on how to fix it? |
TL;DR it's not a bug in We had exactly the same issue with |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I've solved this issue by configuring the QueryDSL import com.querydsl.sql.Configuration;
import com.querydsl.sql.PostgreSQLTemplates;
import com.querydsl.sql.namemapping.ChangeLetterCaseNameMapping;
...
final Configuration configuration = new Configuration(new PostgreSQLTemplates());
configuration.setDynamicNameMapping(new ChangeLetterCaseNameMapping(ChangeLetterCaseNameMapping.LetterCase.LOWER, Locale.ENGLISH)); |
I just spent most of the day investigating whether it's possible to implement this in a database-agnostic way, and I've decided the answer is no. But in case anyone else wants to take a stab at this, I figured I'd summarize my research. At first, it doesn't look so bad. Many databases have some variant of this functionality. But, as always, the devil is in the details:
On top of all that, the JDBC spec creates more headaches if you want to do batched statements. The spec specifically bans executeBatch() from returning any ResultSets. So a batch INSERT with return would have to use batchToBulk = true, and I don't see any generic solution for batch UPDATE/DELETE. For SQL Server, Firebird, and Oracle, there could be a workaround since they can output the data into a variable or temp table. So the batch itself could have no ResultSets and then the actual ResultSet could be read by a separate query. For SQL Server specifically, I've requested the addition of a method in the JDBC driver to allow batch queries to return ResultSets - see microsoft/mssql-jdbc#2156. |
INSERT: http://www.postgresql.org/docs/9.3/static/sql-insert.html
UPDATE: http://www.postgresql.org/docs/9.3/static/sql-update.html
support returning values that were inserted or updated.
In the case of
INSERT
, one could insert a new row and useRETURNING
to retrieve the value that was inserted into anAUTOINCREMENT
column.In the case of UPDATE, one could
UPDATE ... SET ... FROM
and useRETURNING
to retrieve the new values.This syntax is specific to PostgreSQL. It's not clear whether it would be possible to add it to QueryDSL in a database-agnostic manner. Many Stackoverflow answers favor the use of
RETURNING
overCURRVAL()
orLASTVAL()
because the latter are not thread-safe on some databases and return the wrong values if triggers are used. I mention this because there might not be any good alternatives under PostgreSQL.The text was updated successfully, but these errors were encountered: