简体   繁体   English

列是 uuid 类型,但保存实体时表达式是 bytea 类型

[英]Column is of type uuid but expression is of type bytea when saving entity

I have a problem saving entity with UUID field.我在使用 UUID 字段保存实体时遇到问题。 I create a new instance, call setMyGuid(UUID.fromString(guid));我创建一个新实例,调用setMyGuid(UUID.fromString(guid)); and then call save method of my CrudRepository .然后调用我的CrudRepositorysave方法。 But it produces SQL with column value '<byte[]>' , not UUID/String.但它会生成 SQL 列值'<byte[]>' ,而不是 UUID/String。

binding parameter [8] as [BINARY] - [d720f7e2-69ec-4646-bd99-72545123f873]绑定参数 [8] 作为 [BINARY] - [d720f7e2-69ec-4646-bd99-72545123f873]

I have hibernate.dialect set to org.hibernate.dialect.PostgreSQLDialect , I've tried to set also spring.jpa.database-platform , but nothing helps.我将hibernate.dialect设置为org.hibernate.dialect.PostgreSQLDialect ,我也尝试设置spring.jpa.database-platform ,但没有任何帮助。 Here is written , that UUID should map to SQL uuid. 这里写着,UUID应该是map到SQL uuid。

Database is Postgres, column definition is:数据库是 Postgres,列定义是:

@Column(name = "my_guid", nullable = false, unique = true, columnDefinition = "uuid")
private UUID myGuid;

Stack trace with library versions is there:有库版本的堆栈跟踪:

Caused by: org.postgresql.util.PSQLException: ERROR: column "my_guid" is of type uuid but expression is of type bytea
  You will need to rewrite or cast the expression.
  Position: 238
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2676) ~[postgresql-42.5.1.jar:42.5.1]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2366) ~[postgresql-42.5.1.jar:42.5.1]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:356) ~[postgresql-42.5.1.jar:42.5.1]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:496) ~[postgresql-42.5.1.jar:42.5.1]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:413) ~[postgresql-42.5.1.jar:42.5.1]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:190) ~[postgresql-42.5.1.jar:42.5.1]
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:152) ~[postgresql-42.5.1.jar:42.5.1]
    at net.sf.log4jdbc.sql.jdbcapi.PreparedStatementSpy.executeUpdate(PreparedStatementSpy.java:1080) ~[log4jdbc-log4j2-jdbc4.1-1.16.jar:?]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-5.0.1.jar:?]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-5.0.1.jar:?]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3327) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3965) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:80) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:653) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:283) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:264) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:322) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:340) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:184) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:129) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:53) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:737) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:721) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:578) ~[?:?]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:307) ~[spring-orm-6.0.3.jar:6.0.3]
    at jdk.proxy2.$Proxy138.persist(Unknown Source) ~[?:?]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:610) ~[spring-data-jpa-3.0.0.jar:3.0.0]

Thanks for help.感谢帮助。

Problem is in Dialect class. Uuid is registered only if version is >= 8.2, but when I check what getVersion() returns, it's 8.0 (default).问题出在方言 class 中。仅当版本 >= 8.2 时才注册 Uuid,但是当我检查 getVersion() 返回的内容时,它是 8.0(默认)。 PostgreSQL server is 15.1. PostgreSQL 服务器是 15.1。

protected void registerColumnTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
    if (this.getVersion().isSameOrAfter(8, 2)) {
                ddlTypeRegistry.addDescriptor(new DdlTypeImpl(3000, "uuid", this));
    }

You have to create Dialect class and specify version:您必须创建 Dialect class 并指定版本:

public class CustomPostgresDialect extends org.hibernate.dialect.PostgreSQLDialect {

    public CustomPostgresDialect() {
        super(DatabaseVersion.make(15, 1));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 NULL LocalDate 作为输入时,本机命名查询失败,出现异常“列的类型为日期,但表达式的类型为 bytea” - Native named query fails with exception "column is of type date but expression is of type bytea"when NULL LocalDate is given as input 错误:“ publish_date”列的类型为date,但表达式的类型为bytea - ERROR: column “publish_date” is of type date but expression is of type bytea 错误:列“xxxxxxx”的字符类型为[],但表达式的类型为bytea - ERROR: column “xxxxxxx” is of type character varying[] but expression is of type bytea PostgreSQL 使用 JPA 和 Hibernate 抛出“列的类型为 jsonb,但表达式的类型为 bytea” - PostgreSQL throws "column is of type jsonb but expression is of type bytea" with JPA and Hibernate 为什么我不能将实体类的 Point 字段映射到数据库的 Point 字段上? “位置”列的类型为 point 但表达式的类型为 bytea - Why I can't map a Point field of my entity class on a Point field on the database? column “location” is of type point but expression is of type bytea 如何获取带有 bytea 类型 id 列的表? - How to fetch table with bytea type id column? 使用 Spring Data 的 postgres 中的时间戳错误:列 $COLUMN_NAME 的类型是没有时区的时间戳,但表达式的类型是 bytea - Error with timestamp in postgres using Spring Data: column $COLUMN_NAME is of type timestamp without time zone but expression is of type bytea org.postgresql.util.PSQLException:错误:“ geo_detail”列的类型为point,但是表达式的类型为bytea? - org.postgresql.util.PSQLException: ERROR: column “geo_detail” is of type point but expression is of type bytea any solution please? Caused by: org.postgresql.util.PSQLException: ERROR: column "low_range" is of type double precision 但 expression 是类型 bytea - Caused by: org.postgresql.util.PSQLException: ERROR: column "low_range" is of type double precision but expression is of type bytea hibernate-spatial-4.0 创建 bytea 列类型而不是几何 - hibernate-spatial-4.0 creates bytea column type instead of geometry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM