gpt4 book ai didi

java - 不支持从 varbinary 到 BLOB 的转换

转载 作者:行者123 更新时间:2023-12-02 11:58:48 24 4
gpt4 key购买 nike

我正在使用 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方式,制作实体、JpaRepositories,而不是服务来与数据库交互。问题是我无法让 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)

最佳答案

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

关于java - 不支持从 varbinary 到 BLOB 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com