postgresql - 包含来自数据库的唯一编号的非主键业务 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 并生成业务密钥。
解决方案
推荐阅读
- spring - @Repository 和 @EnableJpaRepositories 都不适用于最新的 Hibernate 和 Spring
- android - 为什么立即启动(在 Android 中)时,它没有完成任务?
- typescript - 带有 NODE_PATH 的打字稿定义
- python - Why does my Django Model not work correctly?
- python - Pandas scatter matrix - what do the histograms mean?
- flutter - Is there any way to make one page scroll in flutter?
- c++ - Bidirectional thread communication with multiple condition_variable has rare hang / race condition
- typescript - How to use
and generic types in typescript? - html - Pseudo elemet not showing with overflow-hidden CSS
- javascript - 扩展模式猫鼬中的“虚拟”继承方法不起作用