hibernate在启动时创建空表 - hibernate_sequence [英] hibernate creating empty table - hibernate_sequence at startup

查看:1587
本文介绍了hibernate在启动时创建空表 - hibernate_sequence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


错误:无法读取hi值 - 您需要填充表:hibernate_sequence


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

序列表是因为你如何定义了一个/所有实体的主键。

  @Id 
@ GeneratedValue(strategy = GenerationType.AUTO)//或GenerationType.SEQUENCE
保护长ID;

如果您想使用表格的标识列:

  @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
保护长ID;

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


@GeneratedValue JPA Annotation



在这些教程中经常使用,我们使用@GeneratedValue
注释来让数据库为我们生成一个唯一的主键。
我们在每个例子中都使用了默认的生成类型,但实际上有
有四种不同的策略来让数据库生成主要的
键。这四个选项是:


AUTO IDENTITY TABLE SEQUENCE javax.persistence.GenerationType.AUTO



AUTO生成策略是默认值,这个设置只需
就可以选择主键生成策略,这是缺省的
数据库,通常是IDENTITY,尽管
可能是TABLE或SEQUENCE,数据库是如何配置
的。通常建议使用AUTO策略,因为它使您的代码和应用程​​序最便于携带。
$ b

javax.persistence.GenerationType.IDENTITY



IDENTITY选项只允许数据库为您的应用程序生成一个唯一的
主键。没有序列或表用于
维护主键信息,相反,数据库将
选择一个适当的唯一编号用于Hibernate分配给实体的
主键。使用MySQL,可以选择表格中可用的第一个最低编号的
主键,尽管
这种行为可能因数据库不同而不同。



javax.persistence.GenerationType.Sequence



有些数据库供应商支持使用数据库序列对象
来维护主键。要使用序列,请将
GenerationType策略设置为SEQUENCE,指定生成器
注释的名称,然后提供@SequenceGenerator注释,
具有用于定义序列注释
和数据库中实际序列对象的名称。


So I just downloaded hibernate 5.0.0.1, and I tried my project to it, which is previously using 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

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?

解决方案

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

@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.

这篇关于hibernate在启动时创建空表 - hibernate_sequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆