99

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

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

@GeneratedValue(strategy=GenerationType.AUTO) 

still the error remains.

thomasvdb
  • 739
  • 1
  • 11
  • 32
rParvathi
  • 1,939
  • 2
  • 16
  • 21

17 Answers17

132

You can also put :

@GeneratedValue(strategy = GenerationType.IDENTITY)

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

AUTO_INCREMENT PRIMARY KEY
Kikou
  • 1,895
  • 4
  • 20
  • 32
  • 15
    This might be good in some cases, but has an obvious drawback: every `INSERT` is gonna cause one additional round trip to the Database to fetch the ID. So when this drawback is acceptable, fine. – G. Demecki Mar 18 '16 at 14:28
  • @G.Demecki would you be able to discuss the pros and cons of using hibernates identity generator in contrast to this round trip method? Would be really useful! – Jordan Mackie Jul 10 '18 at 08:22
88

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

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

Om Sao
  • 7,064
  • 2
  • 47
  • 61
rParvathi
  • 1,939
  • 2
  • 16
  • 21
  • where does this get added? – Samuel Thompson Feb 22 '16 at 22:53
  • 1
    add this in hibernate properties. – rParvathi Feb 23 '16 at 06:28
  • JavaDocs of interest - http://docs.jboss.org/hibernate/orm/5.1/javadocs/org/hibernate/cfg/AvailableSettings.html#USE_NEW_ID_GENERATOR_MAPPINGS – muttonUp Jul 19 '16 at 10:37
  • 11
    Your answer might be the solution to the question, but it does not explain why it resolves the problem. Please keep in mind, that links tend to die. – Clijsters Jul 25 '18 at 08:56
  • I know it is a bit late for questions, but what is the difference between jpa.properties.hibernate.id.new_generator_mappings: false and jpa.hibernate.use-new-id-generator-mappings: false? Because with your answers everything works, but with use-new-id-generator-mappings: false don't. I am confused because there are a lot of people on Internet which make they applications work with both. I am using Hibernate 5 with Spring v1.5.6.RELEASE. – Даяна Димитрова Jul 27 '22 at 07:28
57

Working with Spring Boot

Solution

Put the string below in .application.properties

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

Explanation

On Hibernate 4.X this attribute defaults to true.

RivanMota
  • 794
  • 7
  • 14
32

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.

Vishnu S Kumar
  • 666
  • 7
  • 24
  • 1
    should be marked as an answer as this explains things simply - ofcourse an addition of "spring.jpa.properties.hibernate.id.new_generator_mappings=false" should be mentioned but thanks. – nightfury Feb 17 '19 at 13:10
18

FYI

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

Notice that:

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

The following setting worked fine in 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.

To fix this error, the param name must change to:

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

This problem wasted me 2, 3 hours.

And somehow, it looks like there are no document about it.

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

Li Ying
  • 2,261
  • 27
  • 17
15

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

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_mappings property mentioned by @Eva Mariam in my code like this:

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

Clijsters
  • 4,031
  • 1
  • 27
  • 37
Rajan Chauhan
  • 461
  • 2
  • 7
  • 18
9

When you use

@GeneratedValue(strategy=GenerationType.AUTO)

or

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

GenerationType.SEQUENCE as the strategy and that is why it is looking for

schemaName.hibernate_sequence which is a table, for sequence based id generation.

When you use GenerationType.SEQUENCE as the strategy you need to provide the @TableGenerator as follows.

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

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

Kalpa
  • 364
  • 3
  • 5
8

Just in case someone pulls their hair out with this problem like I did today, I couldn't resolve this error until I changed

spring.jpa.hibernate.dll-auto=create

to

spring.jpa.properties.hibernate.hbm2ddl.auto=create
JMoney
  • 171
  • 2
  • 6
5

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

<session-factory>
    ......
    <property name="show_sql">1</property>
    <property name="hibernate.id.new_generator_mappings">false</property>
     ......
 </session-factory>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tekintian
  • 277
  • 3
  • 3
4

If you are using Hibernate version prior to Hibernate5 @GeneratedValue(strategy = GenerationType.IDENTITY) works like a charm. But post Hibernate5 the following fix is necessary.

@Id
@GeneratedValue(strategy= GenerationType.AUTO,generator="native")
@GenericGenerator(name = "native",strategy = "native")
private Long id;

DDL

`id` BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY

REASON

Excerpt from hibernate-issue

Currently, if the hibernate.id.new_generator_mappings is set to false, @GeneratedValue(strategy = GenerationType.AUTO) is mapped to native. If this property is true (which is the defult value in 5.x), the @GeneratedValue(strategy = GenerationType.AUTO) is always mapped to SequenceStyleGenerator.

For this reason, on any database that does not support sequences natively (e.g. MySQL) we are going to use the TABLE generator instead of IDENTITY.

However, TABLE generator, although more portable, uses a separate transaction every time a value is being fetched from the database. In fact, even if the IDENTITY disables JDBC batch updates and the TABLE generator uses the pooled optimizer, the IDENTITY still scales better.

Prasanth Rajendran
  • 4,570
  • 2
  • 42
  • 59
2

You can also put :

@GeneratedValue(strategy = GenerationType.IDENTITY)

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

AUTO_INCREMENT PRIMARY KEY

The above answer helped me.

Chris
  • 3,000
  • 26
  • 43
1

Step 1 : Entry into application.properties

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

Step 2 : Make sure the primary key column should be auto increment type

ALTER TABLE EMPLOYEES MODIFY COLUMN ID INT AUTO_INCREMENT;
Jimmy
  • 995
  • 9
  • 18
0

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

  • Hibernate ORM 5.2.1,
  • Hibernate ORM 5.1.1,
  • Hibernate ORM 5.0.11
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • 1
    I am using Spring-data-jpa which internally is using `Hibernate 5.2.17.Final` as an implementation. I am still getting this issue when `GenerationType` is `AUTO`. – The Coder Jun 26 '18 at 12:20
0

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

    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.

Chinmoy
  • 1,391
  • 13
  • 14
0

In my case, replacing all annotations GenerationType.AUTO by GenerationType.SEQUENCE solved the issue.

wwesantos
  • 71
  • 5
0

CREATE TABLE hibernate_sequence ( next_val bigint DEFAULT NULL ) ;

insert into hibernate_sequence values (1);

Michael Pap
  • 1
  • 1
  • 3
-1

Run this query

create sequence hibernate_sequence start with 1 increment by 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54