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

Hibernate 7元模型生成器丢失Set<String>的泛型类型参数问题

Hibernate 7元模型生成丢失泛型类型问题

在Hibernate 6中,当实体类中被@Column和@NotNull标注的private Set<String> issueTypeLabels字段映射到PostgreSQL的VARCHAR ARRAY列时,生成的元模型代码会保留完整泛型信息:

public static volatile SingularAttribute<TestEntity, Set<String>> issueTypeLabels;

但升级到Hibernate 7后,无论是否给字段添加@JdbcTypeCode(SqlTypes.ARRAY)注解,元模型生成器都会输出原始类型,丢失泛型参数:

public static volatile SingularAttribute<TestEntity, Set> issueTypeLabels;

复现步骤

  • 创建实体类,定义带有@JdbcTypeCode(SqlTypes.ARRAY)注解的Set<String>类型字段

预期结果

生成的元模型属性应为:SingularAttribute<TestEntity, Set<String>>

实际结果

生成的元模型属性为原始类型:SingularAttribute<TestEntity, Set>


示例代码

基类实体

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

    @Id
    private UUID id = UUID.randomUUID();

    public UUID getId() {
        return id;
    }

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

测试实体

@Entity
public class TestEntity extends BaseEntity {

    @Column
    @JdbcTypeCode(SqlTypes.ARRAY)
    private Set<String> tags = new HashSet<>();

    @Column
    private String name;

    public Set<String> getTags() {
        return tags;
    }

    public void setTags(Set<String> tags) {
        this.tags = tags;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

生成的元模型

/**
 * Static metamodel for {@link com.example.hibernateissue.TestEntity}
 **/
@StaticMetamodel(TestEntity.class)
@Generated("org.hibernate.processor.HibernateProcessor")
public abstract class TestEntity_ extends BaseEntity_ {

    
    /**
     * @see #tags
     **/
    public static final String TAGS = "tags";
    
    /**
     * @see #name
     **/
    public static final String NAME = "name";

    
    /**
     * Static metamodel type for {@link com.example.hibernateissue.TestEntity}
     **/
    public static volatile EntityType<TestEntity> class_;
    
    /**
     * Static metamodel for attribute {@link com.example.hibernateissue.TestEntity#tags}
     **/
    public static volatile SingularAttribute<TestEntity, Set> tags;
    
    /**
     * Static metamodel for attribute {@link com.example.hibernateissue.TestEntity#name}
     **/
    public static volatile SingularAttribute<TestEntity, String> name;

}

Gradle构建配置

plugins {
    java
    id("org.springframework.boot") version "4.0.6"
    id("io.spring.dependency-management") version "1.1.7"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
description = "hibernate-issue"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    runtimeOnly("org.postgresql:postgresql")
    annotationProcessor("org.hibernate.orm:hibernate-processor")
    testImplementation("org.springframework.boot:spring-boot-starter-data-jpa-test")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 20:02:27