MySQL 休眠创建空表 - 启动时的 hibernate_sequence
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32424852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate creating empty table - hibernate_sequence at startup
提问by morbidCode
So I just downloaded hibernate 5.0.0.1, and I tried my project to it, which is previously using hibernate 4.3.
所以我刚刚下载了 hibernate 5.0.0.1,并尝试了我的项目,以前使用的是 hibernate 4.3。
When I insert to the database, it gives me this error:
当我插入到数据库时,它给了我这个错误:
ERROR: could not read a hi value - you need to populate the table: hibernate_sequence
错误:无法读取 hi 值 - 您需要填充表:hibernate_sequence
I am using mysql, and my generation strategy is set at GenerationType.auto, and it seems that now hibernate thinks using sequences is the best strategy for generating values. But the table is empty. I think hibernate is atempting to get a value from the sequence but can't find any. But I'm confused because the hibernate_sequence is created by hibernate, shouldn't it provide an initial value?
我用的是mysql,我的生成策略设置在GenerationType.auto,现在hibernate似乎认为使用序列是生成值的最佳策略。但是桌子是空的。我认为 hibernate 试图从序列中获取一个值,但找不到任何值。但是我很困惑,因为 hibernate_sequence 是由 hibernate 创建的,它不应该提供初始值吗?
回答by John Manko
The sequence table is because of how you've defined the primary key on one/all of your entities.
序列表是因为您如何在一个/所有实体上定义主键。
@Id
@GeneratedValue(strategy = GenerationType.AUTO) // or GenerationType.SEQUENCE
protected Long id;
If you want to use a table's identity column:
如果要使用表的标识列:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
You might to check this thread for more information: https://forums.hibernate.org/viewtopic.php?f=1&t=999785&start=0
您可以查看此线程以获取更多信息: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.
@GeneratedValue JPA 注释
在这些教程中,我们经常使用@GeneratedValue 注释让数据库为我们生成唯一的主键。我们在每个示例中都使用了默认的生成类型,但实际上有四种不同的策略可以让数据库生成主键。这四个选项是:
自动身份表序列 javax.persistence.GenerationType.AUTO
AUTO 生成策略是默认值,此设置仅选择作为相关数据库默认值的主键生成策略,通常是 IDENTITY,尽管它可能是 TABLE 或 SEQUENCE,具体取决于数据库的配置方式。通常推荐使用 AUTO 策略,因为它使您的代码和应用程序具有最高的可移植性。
javax.persistence.GenerationType.IDENTITY
IDENTITY 选项仅允许数据库为您的应用程序生成唯一的主键。没有使用序列或表来维护主键信息,相反,数据库只会为 Hibernate 选择一个合适的唯一编号来分配给实体的主键。对于 MySQL,会选择相关表中可用的第一个最小编号的主键,尽管此行为可能因数据库而异。
javax.persistence.GenerationType.Sequence
一些数据库供应商支持使用数据库序列对象来维护主键。要使用序列,请将 GenerationType 策略设置为 SEQUENCE,指定生成器注释的名称,然后提供具有用于定义序列注释名称和实际序列对象名称的属性的 @SequenceGenerator 注释数据库。
回答by Rajitha Yasas
It is Created because Hibernate using that table to track auto increment Id (Next Value of the ID)
创建它是因为 Hibernate 使用该表来跟踪自动增量 Id(ID 的下一个值)
Ex-
前任-
@Id
@GeneratedValue
int id;
回答by Riddhi Gohil
Simple solution :
简单的解决方案:
create hibernate_sequence table as :
创建 hibernate_sequence 表为:
"create sequence <schema/db>.hibernate_sequence"