所以我们的项目使用PostgreSQL数据库,我们使用JPA来操作数据库.
在进行小的更改后,我们的主键值描述为:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "idwebuser", nullable = false)
private Integer idwebuser;
问题是,现在的应用程序并不灵活,因为当我们直接(使用SQL或其他工具)修改数据库而不是通过Java应用程序时,生成的值低于实际的数据库ID值,因此我们在创建新实体时会出错.
JPA是否有可能让数据库自动生成ID,然后在创建过程后获取它?
EDIT
a short note on the answer There was a little lie from my side because we've used a PG Admin -> View first 100 Rows and edited rows from there instead of using select. ANYWAY, it turns out that this editor somehow skips the process of updating the ID and so even in DB when we write a proper INSERT it is EXECUTED with improper ID! So it was basically more a problem of the editor we used than the database and application...
现在,它甚至可以使用@GeneratedValue(strategy=GenerationType.IDENTITY)