@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 value
为id
。
当我在Spring Boot中使用这个类时,它没有像这样创建两个表。仅创建了AdditionType
表。为什么SequenceGenerator
无法在Spring Boot中运行?
答案 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