@GeneratedValue Polymorphic Abstract Superclass over MySQL
When working with abstract superclasses and @GeneratedValue in a Spring MVC application using Hibernate and MySQL, it's essential to consider the specific requirements and limitations of the database. The error "Table 'docbd.hibernate_sequences' doesn't exist" often arises when GenerationType.TABLE is used with MySQL, which does not support sequences natively.
Problem Overview
The issue occurs because GenerationType.TABLE requires a table called "hibernate_sequences" to store the sequence values. Since MySQL does not have this table, Hibernate attempts to create it, resulting in the error. Additionally, using GenerationType.IDENTITY is not feasible when managing IDs in an abstract superclass.
Solution
Despite limitations with GenerationType.TABLE and GenerationType.IDENTITY, there is a solution that addresses both issues. Instead of relying on a sequence table or AUTO_INCREMENT, manually retrieving the next ID and setting it before saving the entity will resolve the problem. This approach involves the following steps:
This approach effectively generates new IDs without relying on sequences or AUTO_INCREMENT. It provides greater flexibility and ensures that the issue of non-existent sequences in MySQL is resolved.
The above is the detailed content of How to Generate IDs for Abstract Superclasses in Spring MVC Applications using Hibernate and MySQL?. For more information, please follow other related articles on the PHP Chinese website!