You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring JPA Hibernate UUID自动生成失败及反射赋值错误求助

Spring JPA Hibernate中String类型UUID主键自动生成异常处理

场景说明

实体类DecileConfig的ID字段要求为String类型,对应数据库表的id字段是VARCHAR(255)类型主键。

初始配置及异常

实体配置代码

@Entity
@Table(name = "decileconfig")
public class DecileConfig extends BaseEntity {

    @Id
    //@GeneratedValue(generator = "UUID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Type(type="org.hibernate.type.UUIDCharType")
    @NotNull(groups = DecileConfig.Existing.class)
    @Null(groups = DecileConfig.New.class)
    @Column(name="id", columnDefinition = "VARCHAR(255)", nullable = false)
    private String id; // This has to be String
..

数据库表创建语句

create table Allocationtest.global_configuration
(
    id varchar(255) NOT NULL PRIMARY key,

插入数据时的异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

判断问题:UUID未自动生成。

更新配置后的新异常

更新后的实体配置代码

@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
@Type(type="org.hibernate.type.UUIDCharType")
@NotNull(groups = DecileConfig.Existing.class)
@Null(groups = DecileConfig.New.class)
@Column(name="id", columnDefinition = "VARCHAR(255)", nullable = false)
private String id;

新异常信息

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Could not set field value [2b221537-1089-4ab3-ae81-c8fbf44bb293] value by reflection : [class com.walmart.sams.services.allocation.configuration.service.model.DecileConfig.id] setter of com.walmart.sams.services.allocation.configuration.service.model.DecileConfig.id; nested exception is org.hibernate.PropertyAccessException: Could not set field value [2b221537-1089-4ab3-ae81-c8fbf44bb293] value by reflection : [class com.walmart.sams.services.allocation.configuration.service.model.DecileConfig.id] setter of com.walmart.sams.services.allocation.configuration.service.model.DecileConfig.id

解决方案

  1. 修正主键生成策略注解
    移除初始配置中的@GeneratedValue(strategy = GenerationType.IDENTITY),该策略依赖数据库自增逻辑,和UUID生成器完全冲突,会导致UUID无法自动生成。确保@GeneratedValue的generator属性和@GenericGenerator的name属性一致。

  2. 解决反射赋值异常
    第二个异常是Hibernate无法通过反射设置id字段值,需检查:

    • 为id字段添加public的setter方法;
    • 若想直接通过字段访问而非属性访问,可添加@Access(AccessType.FIELD)注解明确指定访问模式,让Hibernate直接操作私有字段。
  3. 正确的实体ID配置示例

@Entity
@Table(name = "decileconfig")
public class DecileConfig extends BaseEntity {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
    @Type(type = "org.hibernate.type.UUIDCharType")
    @Column(name = "id", columnDefinition = "VARCHAR(255)", nullable = false)
    @NotNull(groups = DecileConfig.Existing.class)
    @Null(groups = DecileConfig.New.class)
    private String id;

    // 提供public的getter和setter方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    // 其他字段与方法..
}
  1. 核对数据库表结构
    确认数据库表名称(实体配置是decileconfig,建表语句是global_configuration,需保持一致)的id字段为VARCHAR(255)类型主键,未设置自增等冲突属性。

内容的提问来源于stack exchange,提问作者SUVAM ROY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:05:17