mysql - hibernate 创建空表 - 启动时的hibernate_sequence

标签 mysql hibernate jpa sequences

所以我刚刚下载了 hibernate 5.0.0.1,并尝试了我的项目,它以前使用的是 hibernate 4.3。

当我插入数据库时​​,它给我这个错误:

ERROR: could not read a hi value - you need to populate the table: hibernate_sequence

我用的是mysql,我的生成策略设置在GenerationType.auto,看来现在hibernate认为使用序列是最好的生成值策略。但是 table 是空的。我认为 hibernate 试图从序列中获取一个值,但找不到任何值。但是我很困惑,因为 hibernate_sequence 是由 hibernate 创建的,它不应该提供一个初始值吗?

最佳答案

序列表是因为您在一个/所有实体上定义主键的方式。

@Id
@GeneratedValue(strategy = GenerationType.AUTO) // or GenerationType.SEQUENCE
protected Long id;

如果要使用表的标识列:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;

您可以查看此线程以获取更多信息: https://forums.hibernate.org/viewtopic.php?f=1&t=999785&start=0

@GeneratedValue JPA Annotation

Quite often in these tutorials, we have used the @GeneratedValue annotation to have thedatabase generate a unique primary key for us. We have used the default Generation Type in each of our examples, but there are actually four different strategies for having the primary key generated by the database. Those four options are:

AUTO IDENTITY TABLE SEQUENCE javax.persistence.GenerationType.AUTO

The AUTO generation strategy is the default, and this setting simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY, although it might be TABLE or SEQUENCE depending upon how the database is configured. The AUTO strategy is typically recommended, as it makes your code and your applications most portable.

javax.persistence.GenerationType.IDENTITY

The IDENTITY option simply allows the database to generate a unique primary key for your application. No sequence or table is used to maintain the primary key information, but instead, the database will just pick an appropriate, unique number for Hibernate to assign to the primary key of the entity. With MySQL, the first lowest numbered primary key available in the table in question is chosen, although this behavior may differ from database to database.

javax.persistence.GenerationType.Sequence

Some database vendors support the use of a database sequence object for maintaining primary keys. To use a sequence, you set the GenerationType strategy to SEQUENCE, specify the name of the generator annotation, and then provide the @SequenceGenerator annotation that has attributes for defining both the name of the sequence annotation, and the name of the actual sequence object in the database.

关于mysql - hibernate 创建空表 - 启动时的hibernate_sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424852/

相关文章:

php - 拉拉维尔 4 : DB:select returns rows ordered randomly

mysql - SQL select 语句内连接

java - JPA 未获取数据引发 stackoverflow 错误

java - 从数据库中删除已删除的实体

java - 具有分离的 DAO 和服务层的 EJB 的 JPA

php - mysql 全文和类型问题

java.sql.SQLException : Column count doesn't match value count at row 1 error

java - 使用 IN 子句和子选择查询多列的 Hibernate Criteria Query

Hibernate Envers - REVINFO 表不存在

hibernate - 在 JPA 或 HIBERNATE 中直接加入