Extend current SQL translator in EclipseLink (#2383)

* Extend current SQL translator in EclipseLink

* Initialize translator in static block

Signed-off-by: strailov <Stanislav.Trailov@bosch.io>

* translation methods to static

Signed-off-by: strailov <Stanislav.Trailov@bosch.io>

* handle DataIntegrityViolation in rest core

Signed-off-by: strailov <Stanislav.Trailov@bosch.io>

---------

Signed-off-by: strailov <Stanislav.Trailov@bosch.io>
This commit is contained in:
Stanislav Trailov
2025-04-29 17:20:17 +03:00
committed by GitHub
parent 744ab70f97
commit 2a71f61cc2
4 changed files with 38 additions and 5 deletions

View File

@@ -15,6 +15,8 @@ import java.sql.SQLException;
import jakarta.persistence.PersistenceException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLErrorCodes;
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
import org.springframework.lang.NonNull;
import org.springframework.orm.jpa.JpaSystemException;
@@ -54,7 +56,20 @@ class HawkbitEclipseLinkJpaDialect extends EclipseLinkJpaDialect {
@Serial
private static final long serialVersionUID = 1L;
private static final SQLStateSQLExceptionTranslator SQLSTATE_EXCEPTION_TRANSLATOR = new SQLStateSQLExceptionTranslator();
private static final SQLErrorCodeSQLExceptionTranslator SQL_EXCEPTION_TRANSLATOR;
// providing list/set of codes which are not handled from the sql translator properly
private static final String[] DATA_INTEGRITY_VIOLATION_CODES = new String[] {
"1366"
};
static {
SQL_EXCEPTION_TRANSLATOR = new SQLErrorCodeSQLExceptionTranslator();
SQLErrorCodes codes = new SQLErrorCodes();
codes.setDataIntegrityViolationCodes(DATA_INTEGRITY_VIOLATION_CODES);
SQL_EXCEPTION_TRANSLATOR.setSqlErrorCodes(codes);
}
@Override
public DataAccessException translateExceptionIfPossible(@NonNull final RuntimeException ex) {
@@ -84,7 +99,8 @@ class HawkbitEclipseLinkJpaDialect extends EclipseLinkJpaDialect {
if (sqlException == null) {
return null;
}
return SQLSTATE_EXCEPTION_TRANSLATOR.translate("", null, sqlException);
return SQL_EXCEPTION_TRANSLATOR.translate("", null, sqlException);
}
private static SQLException findSqlException(final RuntimeException jpaSystemException) {