Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,10 @@
package org.keycloak.connections.jpa.dialect;

import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.sql.ast.H2SqlAstTranslator;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.insert.InsertSelectStatement;
import org.hibernate.sql.exec.spi.JdbcOperation;

/**
* Fixes Hibernate's H2 dialect to correctly handle HQL {@code insert ... on conflict ... do nothing}
* by emitting a MERGE statement instead of a plain INSERT that silently drops the conflict clause.
* <p>
* See <a href="https://hibernate.atlassian.net/browse/HHH-20826">HHH-20826</a> for the upstream tracker.
* Preserves the configured Keycloak dialect name while using Hibernate's H2 conflict handling.
*/
public class KeycloakH2Dialect extends H2Dialect {

Expand All @@ -43,25 +32,4 @@ public KeycloakH2Dialect() {
public KeycloakH2Dialect(DialectResolutionInfo info) {
super(info);
}

@Override
public SqlAstTranslatorFactory getSqlAstTranslatorFactory() {
return new StandardSqlAstTranslatorFactory() {
@Override
protected <T extends JdbcOperation> SqlAstTranslator<T> buildTranslator(
SessionFactoryImplementor sessionFactory, Statement statement) {
return new H2SqlAstTranslator<>(sessionFactory, statement) {
@Override
protected void visitInsertStatementOnly(InsertSelectStatement statement) {
if (statement.getConflictClause() != null
&& !statement.getConflictClause().getConstraintColumnNames().isEmpty()) {
visitInsertStatementEmulateMerge(statement);
} else {
super.visitInsertStatementOnly(statement);
}
}
};
}
};
}
}
Loading