Java 休眠序列不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32968527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:05:59  来源:igfitidea点击:

Hibernate-sequence doesn't exist

javaspringhibernate

提问by rParvathi

I tried to upgrade hibernate from 4 to 5 in my project with spring 4.2version. After this upgrade, I found the following error in my stack trace when I called a method for updating.

我尝试在我的项目中使用 spring4.2版本将 hibernate 从 4 升级到 5 。这次升级后,当我调用更新方法时,我在堆栈跟踪中发现了以下错误。

10:53:32,185 ERROR TableStructure:149 - could not read a hi value
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist 

I changed the auto incremented Id with annotation

我用注释更改了自动递增的 Id

@GeneratedValue(strategy=GenerationType.AUTO) 

still the error remains.

错误仍然存​​在。

采纳答案by rParvathi

You need to set for Hibernate5.x <property name="hibernate.id.new_generator_mappings">false</property>.. see and link.

您需要为 Hibernate5.x 设置<property name="hibernate.id.new_generator_mappings">false</property>.. 请参阅并链接

For older version of hibernate 4.x: <prop key="hibernate.id.new_generator_mappings">false</prop>

对于旧版本的休眠 4.x: <prop key="hibernate.id.new_generator_mappings">false</prop>

回答by Kikou

You can also put :

你也可以把:

@GeneratedValue(strategy = GenerationType.IDENTITY)

And let the DateBase manage the incrementation of the primary key:

并让 DateBase 管理主键的增量:

AUTO_INCREMENT PRIMARY KEY

回答by Li Ying

FYI

供参考

If you are using hbm files to define the O/R mapping.

如果您使用 hbm 文件来定义 O/R 映射。

Notice that:

请注意:

In Hibernate 5, the param name for the sequence name has been changed.

在 Hibernate 5 中,序列名称的参数名称已更改

The following setting worked fine in Hibernate 4:

以下设置在Hibernate 4 中运行良好:

<generator class="sequence">
    <param name="sequence">xxxxxx_seq</param>
</generator>

But in Hibernate 5, the same mapping setting file will cause a "hibernate_sequence doesn't exist" error.

但是在Hibernate 5 中,相同的映射设置文件会导致“hibernate_sequence 不存在”错误。

To fix this error, the param name must changeto:

要修复此错误,参数名称必须更改为:

<generator class="sequence">
    <param name="sequence_name">xxxxxx_seq</param>
</generator>

This problem wasted me 2, 3 hours.

这个问题浪费了我 2、3 个小时。

And somehow, it looks like there are no documentabout it.

不知何故,似乎没有关于它的文件

I have to read the source code of org.hibernate.id.enhanced.SequenceStyleGenerator to figure it out

我必须阅读 org.hibernate.id.enhanced.SequenceStyleGenerator 的源代码才能弄清楚

回答by Vishnu S Kumar

This is the reason behind this error:

这是此错误背后的原因:

It will look for how the database that you are using generates ids. For MySql or HSQSL, there are increment fields that automatically increment. In Postgres or Oracle, they use sequence tables. Since you didn't specify a sequence table name, it will look for a sequence table named hibernate_sequence and use it for default. So you probably don't have such a sequence table in your database and now you get that error.

它将查找您正在使用的数据库如何生成 id。对于 MySql 或 HSQSL,有自动递增的递增字段。在 Postgres 或 Oracle 中,它们使用序列表。由于您没有指定序列表名称,它将查找名为 hibernate_sequence 的序列表并将其用作默认值。所以你的数据库中可能没有这样的序列表,现在你得到了那个错误。

回答by Rajan Chauhan

I was getting the same error "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mylocaldb.hibernate_sequence' doesn't exist".

我收到了同样的错误“com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'mylocaldb.hibernate_sequence'不存在”。

Using spring mvc 4.3.7 and hibernate version 5.2.9, application is made using spring java based configuration. Now I have to add the hibernate.id.new_generator_mappingsproperty mentioned by @Eva Mariam in my code like this:

使用 spring mvc 4.3.7 和 hibernate 版本 5.2.9,使用基于 spring java 的配置制作应用程序。现在我必须hibernate.id.new_generator_mappings在我的代码中添加@Eva Mariam 提到的属性,如下所示:

@Autowired
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.addProperties(getHibernateProperties());
        sessionBuilder.addAnnotatedClasses(User.class);

        return sessionBuilder.buildSessionFactory();
    }

    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.id.new_generator_mappings","false");
        return properties;
    }

And it worked like charm.

它的作用就像魅力一样。

回答by Vlad Mihalcea

This might be caused by HHH-10876which got fixed so make sure you update to:

这可能是由已修复的HHH-10876引起的,因此请确保您更新到:

  • Hibernate ORM 5.2.1,
  • Hibernate ORM 5.1.1,
  • Hibernate ORM 5.0.11
  • 休眠 ORM 5.2.1,
  • 休眠 ORM 5.1.1,
  • 休眠 ORM 5.0.11

回答by RivanMota

Working with Spring Boot

使用Spring Boot

Solution

解决方案

Put the string below in .application.properties

将下面的字符串放在 .application.properties 中

spring.jpa.properties.hibernate.id.new_generator_mappings=false

Explanation

解释

On Hibernate 4.X this attribute defaults to true.

在 Hibernate 4.X 上,此属性默认为true.

回答by Chinmoy

I added Hibernate sequence in postgres. Run this query in PostGres Editor:

我在 postgres 中添加了 Hibernate 序列。在 PostGres 编辑器中运行此查询:

    CREATE SEQUENCE hibernate_sequence
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 2
  CACHE 1;
ALTER TABLE hibernate_sequence
  OWNER TO postgres;

I will find out the pros/cons of using the query but for someone who need help can use this.

我会找出使用查询的优缺点,但对于需要帮助的人可以使用它。

回答by tekintian

in hibernate 5.x, you should add set hibernate.id.new_generator_mappings to false in hibernate.cfg.xml

在 hibernate 5.x 中,您应该在 hibernate.cfg.xml 中添加 set hibernate.id.new_generator_mappings 为 false

<session-factory>
    ......
    <property name="show_sql">1</property>
    <property name="hibernate.id.new_generator_mappings">false</property>
     ......
 </session-factory>

回答by Kalpa

When you use

当你使用

@GeneratedValue(strategy=GenerationType.AUTO)

@GeneratedValue(strategy=GenerationType.AUTO)

or

或者

@GeneratedValuewhich is short hand way of the above, Hibernate starts to decide the best generation strategy for you, in this case it has selected

@GeneratedValue这是上面的简写方式,Hibernate 开始为你决定最佳的生成策略,在这种情况下它选择了

GenerationType.SEQUENCEas the strategy and that is why it is looking for

GenerationType.SEQUENCE作为战略,这就是为什么它正在寻找

schemaName.hibernate_sequencewhich is a table, for sequence based id generation.

schemaName.hibernate_sequence这是一个表,用于基于序列的 id 生成。

When you use GenerationType.SEQUENCEas the strategy you need to provide the @TableGeneratoras follows.

当您使用GenerationType.SEQUENCE作为策略时,您需要提供@TableGenerator以下内容。

     @Id
     @GeneratedValue(strategy = GenerationType.TABLE, generator = "user_table_generator")
     @TableGenerator(name = "user_table_generator",
                table = "user_keys", pkColumnName = "PK_NAME", valueColumnName = "PK_VALUE")
     @Column(name = "USER_ID")
     private long userId;

When you set the strategy it the to

当您将策略设置为

@GeneratedValue(strategy = GenerationType.IDENTITY).

@GeneratedValue(strategy = GenerationType.IDENTITY).

original issue get resolved because then Hibernate stop looking for sequence table.

原来的问题得到解决,因为然后 Hibernate 停止寻找序列表。