can you list the best methods to add custom objects in schema generation for grails 405 hibernate 5 without installing the migration plugin?

Currently (Grails 4.05) the answer seems to be either use database migration plugin (which is inappropriate for me as outlined in the original question) or use a custom HibernateMappingContextConfiguration in order to inject your own hibernate ‘AuxiliaryDatabaseObject’ into the session factory via overriding the buildSessionFactory() method. These objects are documented in the Hibernate documentation, and the config is mentioned in the GORM documentation.

All attempts at specifying mappings for auxiliary database objects failed. I could not get Gorm / Grails to pay attention to the hibernate configuration and mapping files. It’s unclear or if this is intentional or a bug. Regardless, I needed a different approach.

Fortunately HibernateMappingContextConfiguration has support for directly adding an AuxiliaryDatabaseObject through which you can effectively inject any SQL required to create or drop the auxiliary db objects including functions, views, and even arbitrary sql.

This works for me, and has the advantage that any code injected in this manner carries through (naturally) to the ddl generated when using schema-export, which is necessary when management of the DB (including change set management) is external, with the exported ddl being the produced artifact.

Additionally, this lets you keep your dev environment using ‘create-drop’ for the datasource (so it always starts up from a pristine schema).

for example:

MyHibernateMappingContextConfiguration extends HibernateMappingContextConfiguration {
  SessionFactory buildSessionFactory() throws HibernateException {
    //create and add AuxiliaryDatabaseObject instances as needed
    return super.buildSessionFactory() 

and then just configure hibernate to use your custom context config via application.yml:


CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top