Home > Database > Mysql Tutorial > How to Resolve 'Unknown column 'sequence_name'' Errors When Using @GeneratedValue on a Polymorphic Abstract Superclass with MySQL?

How to Resolve 'Unknown column 'sequence_name'' Errors When Using @GeneratedValue on a Polymorphic Abstract Superclass with MySQL?

Barbara Streisand
Release: 2024-11-22 07:09:14
Original
1005 people have browsed it

How to Resolve

@GeneratedValue Polymorphic Abstract Superclass Over MySQL

In a Spring MVC application using Hibernate and MySQL, you might encounter issues when using @GeneratedValue on an abstract superclass with subclasses that extend it. Here's a closer look at the problem and a solution.

Problem

When saving subclasses of the abstract superclass BaseEntity in MySQL, you may receive an error regarding the non-existence of the hibernate_sequences table. This occurs because MySQL does not support sequences, and GenerationType.TABLE (the default value for @GeneratedValue) requires the use of a sequence.

Solution

To overcome this issue, you need to create a table called hibernate_sequences in MySQL, as suggested by JBNizet in the second edit of the original question. However, even after creating the table, you might encounter another error: "Unknown column 'sequence_name' in 'where clause'".

To resolve this, modify the SELECT statement generated by Hibernate to specify the sequence_name properly:

Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_name = 'BaseEntity' for update
Copy after login

Change the statement to:

Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_names = 'BASE_ENTITY' for update
Copy after login

Ensure that the sequence name is capitalized in the SELECT statement, which should match the sequence name you specified during table creation (create table ... statement).

Once the sequence name is correct, the Hibernate SQL should work seamlessly, allowing you to save subclasses of BaseEntity without encountering "Unknown column 'sequence_name'" errors.

The above is the detailed content of How to Resolve 'Unknown column 'sequence_name'' Errors When Using @GeneratedValue on a Polymorphic Abstract Superclass with MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template