@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
Change the statement to:
Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_names = 'BASE_ENTITY' for update
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!