51

In my spring mvc app, i have the following object. I am trying to make a visual of data using devtool in my app.

@Entity
@Data
public class ConsultationRequest {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String email;

    private String purpose;

    private String programme;

    private int year;

    private String language;

    private String comments;
    @Enumerated(EnumType.STRING)
    private ConsultationStatus status;
}

Then i used the jpa to make the entity:

@Repository
public interface ConsultationRequestRepository extends JpaRepository<ConsultationRequest, Long> {

}

The problem is when i load my application, i face with 2 errors:

 Unsuccessful: drop sequence hibernate_sequence
[36morg.hibernate.tool.hbm2ddl.SchemaExport  Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:

Then when i open the

http://localhost:8080/h2-console/

I cannot see the table. It seems that the in the boot process, table is not made.

12 Answers 12

98

Update your code as below:

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

As you have not specified a sequence table name, hibernate will look for a sequence table named as hibernate_sequence and use it as default.

For Oracle/Postgres, increment fields used are sequence tables.
In MySql, there are increment fields that automatically increment.

Sign up to request clarification or add additional context in comments.

Comments

29

If someone is getting this error with Spring Boot testing(with H2) , we have to use following in the application.properties(or whatever profile we are using) :

spring.jpa.hibernate.ddl-auto=create-drop

2 Comments

I actually use the property: spring.jpa.hibernate.ddl-auto=update. That doesn't override the data in case they were inserted with non-generated keys in data.sql
create-drop worked for POSTing data but not for loading from data.sql during startup with H2 database. The update value worked the opposite (worked fro data.sql but not for POST data) and the none value was the same as update
13

Setting the following property in the properties file helped me solve the hibernate_sequence problem for hibernate 5.4v

spring:
  jpa:
    hibernate:
      use-new-id-generator-mappings: false

2 Comments

I had already wasted half a day on this, and tried everything. Nothing worked, until I have found this solution. When testing my DB Code, I use Liquibase to create the DB schema, and populate it with some data. In my tests, I was always getting this error: Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "HIBERNATE_SEQUENCE" not found; SQL statement This was the fix: spring.jpa.hibernate.use-new-id-generator-mappings=false
This also was the solution for me. I use liquibase as well to create the database tables with an intended target of PostgreSQL, and H2 for testing. The auto-increment works in PostgreSQL by creating a sequence, but not in H2. This was the solution for me, as the H2 db is not persistent and I just need a new next sequence ID for testing.
9

Check persistence.xml

property name="hibernate.hbm2ddl.auto" value="create"

not hdm2ddl

This worked in my case.

Comments

3

If you use a 2nd cache with liquidbase, you have to add the sequence in the changelog like this:

<changeSet author="liquibase-docs"
    id="createSequence-example">
    <createSequence catalogName="cat" cycle="false"
        incrementBy="1" ordered="true" schemaName="public"
        sequenceName="hibernate_sequence" startValue="0" />
</changeSet>

Comments

3

SQL operation ERROR when start app spring-boot.

I added the setting in spring properties and solved: in the spring:jpa.hibernate.ddl-auto= create-drop to hibernate be able create or drop table automatically.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
3

spring.jpa.hibernate.use-new-id-generator-mappings=false is no longer supported, use this instead

spring.jpa.properties.hibernate.id.db_structure_naming_strategy=legacy

Comments

0

For spring-boot 2.7.x and h2 2.x you need to add MODE=LEGACY; in the database connection:

example application.yml:

spring:
  datasource:
    url: jdbc:h2:mem:test;MODE=LEGACY;

exemple application.properties:

spring.datasource.url=jdbc:h2:mem:test;MODE=LEGACY;

Comments

0

I ran into this issue while running some tests. Adding these annotations helped me fix the issue for me:

@Transactional
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)

Comments

-1

If someone is having this problem while testing a repository, changing the code like below might help you:

From this:

@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

To this:

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

Comments

-1
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

with

spring.jpa.hibernate.use-new-id-generator-mappings=false

in test-config.properties works for me

Comments

-2

For Mysql:

the auto-increment is not added, modify ur table:

ALTER TABLE table_name MODIFY COLUMN id BIGINT AUTO_INCREMENT=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.