Add executeWithKey() to JPAInsertClause and HibernateInsertClause#1693
Open
zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
Open
Add executeWithKey() to JPAInsertClause and HibernateInsertClause#1693zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
Conversation
b9a1975 to
2308d3e
Compare
Contributor
|
@zio0911 Shouldn't a similar approach to escaping table and column names as in NativeSQLSerializer be used in JpaInsertNativeHelper? See for example: getTemplates().quoteIdentifier(column.name(), precededByDot). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
executeWithKey(Path<T>)andexecuteWithKey(Class<T>)toJPAInsertClauseandHibernateInsertClauseStatement.RETURN_GENERATED_KEYSto retrieve auto-generated keysJpaInsertNativeHelperutility to resolve@Table/@Columnannotations and build native SQL INSERT statementsdoReturningWork()toSessionHolderinterface and all implementationsCloses #1692
Motivation
The SQL module's
SQLInsertClausesupportsexecuteWithKey(), but the JPA module does not. This forces JPA users to fall back toEntityManager.persist()+flush()for insert + key return, breaking the consistent QueryDSL style.Using the SQL module in a JPA project requires a separate
SQLQueryFactory, SQL-specific Q-classes, and managing two query factories — excessive overhead just for insert key return.Before / After
Before — must break out of QueryDSL:
After — stays in QueryDSL:
Implementation
Since JPA's
Query.executeUpdate()only returns affected row count, the implementation:@Table/@Columnannotations to build native SQL (same pattern asNativeSQLSerializer)HibernateInsertClauseusesSession.doReturningWork()for JDBC accessJPAInsertClauseusesEntityManager.unwrap(Session.class).doReturningWork()Limitations
INSERT ... SELECTsubqueries are not supported (throwsUnsupportedOperationException)@Table/@Columnannotations if using a custom HibernatePhysicalNamingStrategyJPAInsertClausecurrently relies on Hibernate as the JPA providerTest plan
JpaInsertNativeHelper(table name resolution, column name resolution, SQL generation)HibernateInsertClause.executeWithKey()(set style, columns/values style, class type, multiple inserts, column annotation, subquery rejection)JPAInsertClause.executeWithKey()(same scenarios)./mvnw -Pdev verifypasses)git-code-formatpre-commit hook)