如何让 Hibernate 使用 setFixedCHAR 而不是 setString

How to make Hibernate use setFixedCHAR instead of setString

我能否以某种方式修改 Hibernate 将参数绑定到查询的方式?

例如,我希望 hibernate 在字符串列上执行时使用 OracleResultSet.setFixedChar(),而不是在通过 Spring 数据执行 JPA 查询时使用 rs.setString()

如果没有 Hibernate,我会这样做:

try(PreparedStatement ps = con.executeQuery("...")) {
   if(ps.isWrapped(OraclePreparedStatement.class) {
      ps.unwrap(OraclePreparedStatement.class).setFixedCHAR(0, myStringField);
   } else {
      ps.setString(0, myStringField);
   }

   try(ResultSet rs = ps.getResultSet()) {
      while(rs.next()) {
         ... do stuff ...
      }
   }
}

存储库方法(Spring 数据 JPA):

List<Object> findByMyStringField(String myStringField);

如何影响 Hibernate 绑定变量的方式。在上面的示例中,始终使用 setString

作为背景:问题是我们所有的旧版数据库都使用 CHAR 列而不是 VARCHAR2,所以我们必须处理空格,setFixedCHAR 应该完全按照我们的意愿进行想要。

通过实施 SqlTypeDescriptor 和自定义方言找到解决方案:

  @Autowired
  private DataSource source;

  @Bean
  public HibernateJpaVendorAdapter getHibernateJPAVendorAdapter() {
    return new CustomHibernateJpaVendorAdaptor();
  }

  private static class CustomHibernateJpaVendorAdaptor extends HibernateJpaVendorAdapter {

    @Override
    protected Class<?> determineDatabaseDialectClass(Database database) {
      // if HSQL is copied from Spring Sourcecode to keep everything the same
      if (Database.HSQL.equals(database)) {
        return CustomHsqlDialect.class;
      }
      try {
        if (source.isWrapperFor(OracleDataSource.class)) {
          return CustomOracleDialect.class;
        }
      } catch (SQLException e) {
      }
      return super.determineDatabaseDialectClass(database);
    }

    private class CustomHsqlDialect extends HSQLDialect {

      public CustomHsqlDialect() {
        registerColumnType(Types.BOOLEAN, "boolean");
        registerHibernateType(Types.BOOLEAN, "boolean");
      }
    }
  }

  @NoArgsConstructor
  public static class CustomOracleDialect extends Oracle12cDialect {

    private static final OracleCharFix INSTANCE = new OracleCharFix();


    @Override
    protected SqlTypeDescriptor getSqlTypeDescriptorOverride(final int sqlCode) {
      switch (sqlCode) {
        case Types.VARCHAR:
          return INSTANCE;
        default:
          return super.getSqlTypeDescriptorOverride(sqlCode);
      }
    }
  }

  @Slf4j
  private static class OracleCharFix extends CharTypeDescriptor {

    @Override
    public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
      return new BasicBinder<>(javaTypeDescriptor, this) {
        @Override
        protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
          throws SQLException {
          if (st.isWrapperFor(OraclePreparedStatement.class)) {
            OraclePreparedStatement ops = st.unwrap(OraclePreparedStatement.class);
            if (ops.getParameterMetaData().getParameterType(index) == Types.CHAR) {
              ops.setFixedCHAR(index, javaTypeDescriptor.unwrap(value, String.class, options));
            } else {
              st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
            }
          } else {
            st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
          }
        }

        @Override
        protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
          throws SQLException {
            //Is nolonger used by Hibernate in the current Version
            st.setString(name, javaTypeDescriptor.unwrap(value, String.class, options));
        }

        private boolean checkIfCHARByName(ResultSetMetaData metadata, String name)
          throws SQLException {
          for (int i = 1; i <= metadata.getColumnCount(); i++) {
            if (metadata.getColumnType(i) == Types.CHAR && Objects.equals(metadata.getColumnName(i), name)) {
              return true;
            }
          }
          return false;
        }
      };
    }