Spring Boot Hibernate 6.2.5 Final,审计表问题

rvpgvaaj  于 2023-10-16  发布在  Spring
关注(0)|答案(1)|浏览(194)

我已经从hibernate5迁移到hibernate6了,在这次迁移中我发现了一个主要问题,即在db中保存uuid。在H5中,我们有@Type(type="uuid-char"),在h6中,我替换为@JdbcTypeCode(SqlTypes.VARCHAR),它工作了。但问题是审计表。审计表给了我不正确的字符串excpetion..
这是我的实体类

`import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.envers.AuditTable;
import org.hibernate.envers.Audited;
import org.hibernate.type.SqlTypes;
import sg.customs.ntp.rsrf.commonservice.base.model.BaseModel;

import java.util.UUID;

@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "tbl_sample", schema = "sample_schema", catalog = "sample_schema")
@JsonIgnoreProperties(ignoreUnknown = true, value = {"hibernateLazyInitializer", "handler"})
@Audited
@AuditTable(value = "tbl_sample_audit", schema = "sample_schema", catalog = "sample_schema")
public class SampleModel extends BaseModel {
    @Id
    @Column(name = "SampleId")
    @JdbcTypeCode(SqlTypes.VARCHAR)
    private UUID sampleId = UUID.randomUUID();
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CompanyId")
    private CompanyModel company;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "UserId")
    private UserModel user;

    @Column(name = "Email", length = 300)
    private String email;

}
`

当jpa持久化到数据库时,我得到了这个错误.

Caused by: java.sql.SQLException: Incorrect string value: '\xE2\x91\xF5V\xA5F...' for column 'SampleId' at row 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:130)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1320)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:994)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:275)
    ... 136 more
2nc8po8w

2nc8po8w1#

设法解决这个问题与自定义属性转换器。我不知道为什么h6 envers忽略了@JdbcType注解,但这应该是一样的:

@Converter(autoApply = true)
public class UuidConverter implements AttributeConverter<UUID, String> {

    @Override
    public String convertToDatabaseColumn(final UUID attribute) {
        return attribute == null ? null : attribute.toString();
    }

    @Override
    public UUID convertToEntityAttribute(final String dbData) {
        if (StringUtils.isBlank(dbData)) {
            return null;
        } else {
            return UUID.fromString(dbData);
        }
    }

}

相关问题