首页 > 解决方案 > 包含来自数据库的唯一编号的非主键业务 ID

问题描述

我正在使用 JPA、Hibernate 和 Postgres。我希望代码尽可能与解决方案中立,其中 JPA 是给定的。我的简化实体如下所示:

@Entity
@Table(name = "example")
public class ExampleEntity  {

    @Id
    @GeneratedValue
    private UUID id;
    
    private String businessId; //format is YYYY000001 where YYYY = current year. Current assumption: number is incrementing and reset every year, number is always filled up with leading 0 to make the key 10 digits
}

我总是有一个生成的 UUID 作为主键。只有在达到某个状态时才应设置业务 ID,因此与创建实体的时间无关。我希望数据库处理递增的数字。

最好我想通过 JPA 解决这个问题,但也看到一个“更脏”的解决方案,我在我的逻辑中获取序列 ID 并生成业务密钥。

标签: postgresqlhibernatejpa

解决方案


推荐阅读