Home >Database >Mysql Tutorial >How to Generate IDs for Abstract Superclasses in a Spring MVC Application Using Hibernate and MySQL?
Polymorphic Abstract Superclass and MySQL Identity Generation
In a Spring MVC application employing Hibernate and MySQL, the challenge of managing IDs for abstract superclasses and their subclasses arises. GenerationType.IDENTITY is not compatible with abstract superclasses, and MySQL lacks support for sequences. How can this issue be resolved?
Firstly, it's important to understand MySQL's limitation: it cannot generate IDs and insert records simultaneously. Hibernate, however, expects this capability during entity insertions.
To address this, a "LAST_IDS" table can be implemented. This table tracks the last assigned IDs for each relevant entity. The following steps outline the process for generating and persisting IDs using this approach:
By utilizing this approach, ID generation and management become compatible with abstract superclasses and MySQL's limitations. It is noteworthy that inheritance in Hibernate entities should generally only be employed when supporting inheritance relationships in the database, such as PostgreSQL or Oracle.
The above is the detailed content of How to Generate IDs for Abstract Superclasses in a Spring MVC Application Using Hibernate and MySQL?. For more information, please follow other related articles on the PHP Chinese website!