Spring Boot:全局设置 Hibernate 序列

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

Spring Boot: Set hibernate sequence globally

问题

  1. @Entity
  2. data class Task(@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
  3. var id: Int = 0,
  4. @ManyToOne(optional = false) @OnDelete(action = OnDeleteAction.CASCADE)
  5. var exercise: Exercise = Exercise(),
  6. @Column(nullable = false)
  7. var name: String = "")
  8. @Entity
  9. data class Exercise(@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
  10. var id: Int = 0,
  11. @Column(nullable = false)
  12. var count: Int = 0)

使用这个示例,所有的表都使用相同的序列:hibernate_sequence

如果我想进行配置,例如设置自定义的 allocationSize,我必须在每个表中都定义,对吗?

@SequenceGenerator(name = "task_seq", sequenceName = "task_seq", allocationSize = 100)

是否有一个 Bean 或其他内容?因为我喜欢为所有表使用相同的序列的想法。

英文:

Currently I define the tables the following way:

The example is in Kotlin, but you can answer in Java if you want.

  1. @Entity
  2. data class Task(@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
  3. var id: Int = 0,
  4. @ManyToOne(optional = false) @OnDelete(action = OnDeleteAction.CASCADE)
  5. var exercise: Exercise = Exercise(),
  6. @Column(nullable = false)
  7. var name: String = "")
  8. @Entity
  9. data class Exercise(@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
  10. var id: Int = 0,
  11. @Column(nullable = false)
  12. var count: Int = 0)

Using this example all tables are using the same sequence: hibernate_sequence.

If I want to configure it e.g. setting a custom allocationSize , then I must define it in every table, am I right?

@SequenceGenerator(name = "task_seq", sequenceName = "task_seq", allocationSize = 100)

Is there a Bean or anything else? Because I like the idea of using the same sequence for all tables.

答案1

得分: 1

  1. 您可以拥有一个带有Id的抽象基类
  2. import javax.persistence.GeneratedValue;
  3. import javax.persistence.GenerationType;
  4. import javax.persistence.Id;
  5. import javax.persistence.MappedSuperclass;
  6. @MappedSuperclass
  7. public abstract class BaseEntity {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.SEQUENCE)
  10. private Long id;
  11. public Long getId() {
  12. return id;
  13. }
  14. public void setId(Long id) {
  15. this.id = id;
  16. }
  17. }
  18. 每个实体将会继承这个基类
英文:

You can have an abstract base class with Id like :

  1. import javax.persistence.GeneratedValue;
  2. import javax.persistence.GenerationType;
  3. import javax.persistence.Id;
  4. import javax.persistence.MappedSuperclass;
  5. @MappedSuperclass
  6. public abstract class BaseEntity {
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.SEQUENCE)
  9. private Long id;
  10. public Long getId() {
  11. return id;
  12. }
  13. public void setId(Long id) {
  14. this.id = id;
  15. }
  16. }

and every entity will extend this.

huangapple
  • 本文由 发表于 2020年4月4日 21:16:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61028673.html
匿名

发表评论

匿名网友

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

确定