I'm making a Spring boot application with Hibernate ORM framework.
I have an @Entity
class in my code:
@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;
I successfully can persist a Thing
to my datasource. And thing_id
is generated as expected.
But I cannot insert a record directly in database manager (e.g. DataGrip):
insert into thing (name, price)
values ('aaa', 123);
I have a warning and appropriate error when trying to insert:
Following columns have no computed/default value and must be listed explicitly: thing_id
So how can I make a column value autogenerated during insertion directly in database manager?
CodePudding user response:
If you have a numeric column that you want to auto-increment, it might be an option to set columnDefinition directly. This has the advantage, that the schema auto-generates the value even if it is used without hibernate. This might make your code db-specific though:
@Column(columnDefinition = "serial") // postgresql
@Column(columnDefinition = "integer auto_increment") // MySQL