有没有一种方法来描述Spring Hibernate中每一行的唯一序列?

huangapple go评论72阅读模式
英文:

Is there a way to describe unique sequence per each row in Spring Hibernate?

问题

我尝试过使用复合键,但有以下问题:

  1. 复合键不支持自动生成。
  2. @IdClass存在问题(Spring无法构造long类)。
  3. @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:

  1. Composite keys doesn't work with auto generation
  2. @IdClass is broken (long class couldn't be constructed by Spring)
  3. @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>



huangapple
  • 本文由 发表于 2020年8月5日 18:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定