为什么这个序列生成器不在Spring Boot中创建数据库表?

时间:2017-08-20 02:58:21

标签: java mysql spring jpa spring-boot

@Entity
@Table(name = "addition_type")
public class AdditionType {
    @Id
    @SequenceGenerator(name = "addition_type_id_seq", sequenceName = "addition_type_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "addition_type_id_seq")
    @Column(name = "id")
    private int id;

    @Column(name = "code")
    private String code;

    @Column(name = "name")
    private String name;

    @Column(name = "sinhala_name")
    private String sinhalaName;

    @Column(name = "report_name")
    private String reportName;

    @Column(name = "status")
    private int status;

    @ManyToOne
    @JoinColumn(referencedColumnName = "id")
    private CompanyInfo companyInfo;

    // there are not show getter and setter
}

当我在Spring中使用这个类时,生成了两个数据库表:实体的AdditionType表和与序列生成器对应的addition_type_id_seq表。 AdditionType表包含以下列:id,name,code ...,另一个表的next valueid

当我在Spring Boot中使用这个类时,它没有像这样创建两个表。仅创建了AdditionType表。为什么SequenceGenerator无法在Spring Boot中运行?

1 个答案:

答案 0 :(得分:2)

由于MYSQL方言不支持序列:

我们可以利用表生成器策略来定义序列:

    @Id
    @TableGenerator(name = "addition_type_id_seq", allocationSize = 1, table = "ADDITION_TYPE_SEQUENCES",
                pkColumnName = "SEQ_NAME",
                        valueColumnName = "SEQ_NUMBER",
                pkColumnValue = "SEQUENCE")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "addition_type_id_seq")
    @Column(name = "id")
    private int id;

这将确保创建两个表:ADDITION_TYPE_SEQUENCES和ADDITION_TYPE