英文:
Is there a way to describe unique sequence per each row in Spring Hibernate?
问题
我尝试过使用复合键,但有以下问题:
- 复合键不支持自动生成。
- @IdClass存在问题(Spring无法构造long类)。
- @EmbeddedId可行,但仍然存在第一问题。
基本上,我想要做的是,每一行(board link)都有唯一的序列,就像这样:
+---------+-----------+------------+
| post_id | thread_id | board_link |
+---------+-----------+------------+
| 1 | 1 | board_1 |
| 2 | 1 | board_1 |
| 3 | 1 | board_1 |
| 1 | 2 | board_2 |
| 2 | 2 | board_2 |
| 3 | 3 | board_2 |
+---------+-----------+------------+
我正在使用Spring Boot 2.2.6.RELEASE,PostgreSQL 12.3-2(9.6 testcontainers)和6.3.2 flyway-core。都是通过gradle构建。
英文:
I've tried using composite keys - but:
- Composite keys doesn't work with auto generation
- @IdClass is broken (long class couldn't be constructed by Spring)
- @EmbeddedId works but №1 issue is still there
Basically what i want to do, is make unique sequence per row (board link), like this:
+---------+-----------+------------+
| post_id | thread_id | board_link |
+---------+-----------+------------+
| 1 | 1 | board_1 |
| 2 | 1 | board_1 |
| 3 | 1 | board_1 |
| 1 | 2 | board_2 |
| 2 | 2 | board_2 |
| 3 | 3 | board_2 |
+---------+-----------+------------+
I'm using Spring Boot 2.2.6.RELEASE, PostgreSQL 12.3-2 (9.6 testcontainers) and 6.3.2 flyway-core. All through gradle.
答案1
得分: 1
你可以使用 GeneratedValue
注解,并将策略设置为 GenerationType.SEQUENCE
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
英文:
You can use the GeneratedValue
annotation and set the strategy as GenerationType.SEQUENCE
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论