JPA/Hibernate 具有外部生成的标识符

JPA/Hibernate with externally generated identifiers

我希望通过 Spring Boot 2.5 公开一个遗留关系模式,其中主键由 DBMS 根据身份字段按照以下几行计算:

create table items
(

    serial      int identity,

    id          as '/items/' + convert(varchar(12), serial) persisted primary key,

    …

)

我的问题是 JPA/Hibernate 似乎假设主键总是在服务器端生成,我们 don't apparently have a suitable generation strategy:

@Entity
@Table(name="items")
public static class ItemEntity {

    @Id
    @GeneratedValue(strategy="{???}")
    private String id;

    …

}

是否有可能指示 JPA/Hibernate 只接受生成的值而不尝试写入它?

这正是 IDENTITY 策略的用途。该策略假设ID是在数据库中生成的。

@Entity
@Table(name="items")
public static class ItemEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String handle;

    …

}