我正在制作一个使用Hibernate ORM框架的Spring启动应用程序。
我有一个@Entity
类在我的代码:
@Entity
@Table(name = "thing")
public class Thing {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "thing_id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "price")
private int price;
我成功地将Thing
持久化到我的数据源。生成thing_id
但是我不能直接在数据库管理器中插入记录(例如DataGrip):
insert into thing (name, price)
values ('aaa', 123);
我有一个警告和适当的错误,当尝试插入:
Following columns have no computed/default value and must be listed explicitly: thing_id
那么我如何使列值自动生成插入期间直接在数据库管理器?
如果您有一个想要自动递增的数值列,可以直接设置columnDefinition。这样做的好处是,即使在不使用hibernate的情况下,模式也会自动生成值。这可能会使您的代码特定于db:
@Column(columnDefinition = "serial") // postgresql
@Column(columnDefinition = "integer auto_increment") // MySQL