szab.kel
szab.kel

Reputation: 2526

The conversion from varbinary to BLOB is unsupported

I am using SQL Server 2016, Spring Boot 1.5.8, Hibernate 5.2 and Spring Data JPA with 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;
}

I am using the standard spring way, made Entities, JpaRepositories, than Services to interact with the db. The problem is I cannot get byte[] mapping to work on my entities. Any interaction through Hibernate throws an exception:

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

The entity I am talking about:

@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;

    ...

The column created on the database is correct, VARBINARY(16) NOT NULL, but when I try to call the repository from the service like rdJsonStoreRepository.findAll(new PageRequest(0,10)) I get en error:

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)

Upvotes: 0

Views: 6343

Answers (2)

Faouzi
Faouzi

Reputation: 1019

For me, the above solution didn't work, so I did the following workarround :

I made Hibernate create a new database for me using spring.jpa.hibernate.ddl-auto=create from my objects. Then I imported my old database;

PS: in my situation i have migrated from mySQL to SQL Server database.

Upvotes: 0

szab.kel
szab.kel

Reputation: 2526

I had to remove @Lob from the byte[] field. @Lob only supposed to work with varbinary(max) (and other supported bigger column types).

Upvotes: 3

Related Questions