简体   繁体   English

Spring 引导:全局设置 hibernate 序列

[英]Spring Boot: Set hibernate sequence globally

Currently I define the tables the following way:目前我通过以下方式定义表格:

The example is in Kotlin, but you can answer in Java if you want.示例在 Kotlin 中,但如果需要,您可以在 Java 中回答。

@Entity
data class Task(@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
                var id: Int = 0,

                @ManyToOne(optional = false) @OnDelete(action = OnDeleteAction.CASCADE)
                var exercise: Exercise = Exercise(),

                @Column(nullable = false)
                var name: String = "")

@Entity
data class Exercise(@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
                    var id: Int = 0,

                    @Column(nullable = false)
                    var count: Int = 0)

Using this example all tables are using the same sequence: hibernate_sequence .使用此示例,所有表都使用相同的序列: hibernate_sequence

If I want to configure it eg setting a custom allocationSize , then I must define it in every table, am I right?如果我想配置它,例如设置自定义allocationSize ,那么我必须在每个表中定义它,对吗?

@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.因为我喜欢对所有表使用相同序列的想法。

You can have an abstract base class with Id like:您可以拥有一个抽象基 class ,其 ID 如下:

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class BaseEntity {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}

and every entity will extend this.每个实体都会扩展它。

That works but in the case of using hibernate Envers it would create two sequences.这可行,但在使用 hibernate Envers 的情况下,它将创建两个序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM