【问题标题】:The conversion from varbinary to BLOB is unsupported不支持从 varbinary 到 BLOB 的转换
【发布时间】:2017-11-21 06:52:26
【问题描述】:

我正在使用 SQL Server 2016、Spring Boot 1.5.8、Hibernate 5.2 和带有 Hikari 2.7 的 Spring Data JPA。

# HIKARI
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.idle-timeout=10000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.maximum-pool-size=8
spring.datasource.hikari.auto-commit=false

# DATABASE
spring.datasource.url=jdbc:sqlserver://100000001:1433;databaseName=Asdf
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.continue-on-error=true


# JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=com......config.SqlServer2012CustomDialect
spring.jpa.database=sql_server
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.cache.use_second_level_cache=false
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.hibernate.use-new-id-generator-mappings=true

public class SqlServer2012CustomDialect extends SQLServer2012Dialect {
    public SqlServer2012CustomDialect() {
        registerColumnType(Types.VARBINARY, FINGERPRINT_COLUMN_LENGTH, FINGERPRINT_COLUMN);
        registerColumnType(Types.VARBINARY, "VARBINARY(MAX)" );
        //registerColumnType(Types.VARBINARY, "BLOB");
        //registerColumnType(Types.VARBINARY, "BINARY");
    }
}

public final class DatabaseColumnDefinitions {
    private DatabaseColumnDefinitions(){}

    public static final String FINGERPRINT_ENCODING = "UTF-8";
    public static final Charset FINGERPRINT_ENCODING_CHARSET = Charset.forName(FINGERPRINT_ENCODING);

    public static final String FINGERPRINT_COLUMN = "VARBINARY(16)";
    public static final int FINGERPRINT_COLUMN_LENGTH = 16;
}

我使用的是标准的spring方式,制作了Entities,JpaRepositories,而不是Services来与db交互。问题是我无法让byte[] 映射在我的实体上工作。通过 Hibernate 进行的任何交互都会引发异常:

com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from varbinary to BLOB is unsupported.

我说的实体:

@Entity
@Table(
        uniqueConstraints = {
                @UniqueConstraint(name = "rdjsonstore_fingerprint_unique",columnNames = "fingerprint")
        }
)
public class RDJsonStore implements Serializable, FingerprintIndexer {

    @Column(nullable = false)
    @Id
    @GeneratedValue(generator = "rdjsonstore_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "rdjsonstore_sequence", sequenceName = "rdjsonstore_sequence", allocationSize = 10000)
    private Long id;

    @Lob
    @Basic
    @Column(nullable = false, length = FINGERPRINT_COLUMN_LENGTH, columnDefinition = FINGERPRINT_COLUMN)
    private byte[] fingerprint;

    ...

在数据库上创建的列是正确的,VARBINARY(16) NOT NULL,但是当我尝试从rdJsonStoreRepository.findAll(new PageRequest(0,10)) 之类的服务调用存储库时,我得到了错误:

ERROR 4980 --- [p-nio-80-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : The conversion from varbinary to BLOB is unsupported.
org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy128.findAll(Unknown Source)

【问题讨论】:

    标签: java sql-server spring hibernate jpa


    【解决方案1】:

    我不得不从byte[] 字段中删除@Lob@Lob 只适用于 varbinary(max)(以及其他受支持的更大的列类型)。

    【讨论】:

      【解决方案2】:

      对我来说,上述解决方案不起作用,所以我做了以下解决方法:

      我让 Hibernate 使用我的对象中的 spring.jpa.hibernate.ddl-auto=create 为我创建了一个新数据库。然后我导入了我的旧数据库;

      PS:在我的情况下,我已经从 mySQL 迁移到 SQL Server 数据库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 2021-09-26
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        • 2011-10-16
        相关资源
        最近更新 更多