-1

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

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

如果没有 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总是使用。

作为背景:问题是我们所有的 Legacy DB 都使用CHAR列而不是VARCHAR2,所以我们必须处理空格并且setFixedCHAR应该完全按照我们的意愿行事。

4

1 回答 1

0

通过实现 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;
        }
      };
    }
于 2019-02-21T15:53:29.013 回答