从varbinary到BLOB的转换不受支持

时间:2017-11-21 06:52:26

标签: java sql-server spring hibernate jpa

我使用的是SQL Server 2016,Spring Boot 1.5.8,Hibernate 5.2和Spring Data JPA以及Hikari 2.7。

# 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)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

对我来说,上述解决方案无效,因此我做了以下工作:

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

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