简体   繁体   English

如何在不需要迁移插件的情况下使用 Grails 4.0.5 (Hibernate 5) 在模式生成期间添加自定义 db 对象?

[英]How can I add custom db objects during schema generation with Grails 4.0.5 (Hibernate 5) without the need for migration plugin?

I need to add a couple functions and views that are part of the system schema design.我需要添加一些作为系统架构设计一部分的函数和视图。 This should be part of the session start up (especially in development and test) because of the dbCreate create-drop settings on the data source so that the initial dev & test environments are initially set up consistently & pristine.这应该是会话启动的一部分(特别是在开发和测试中),因为数据源上的 dbCreate create-drop 设置,以便初始开发和测试环境最初设置一致和原始。

It does not make sense to use the database migration plugin because these are predefined db objects that are part of the whole system design and do not belong in a change set unless they themselves are changing.使用数据库迁移插件没有意义,因为这些是预定义的 db 对象,它们是整个系统设计的一部分,不属于更改集,除非它们本身正在更改。 It's equivalent to requiring us to add the domain table generation sql in a change set.相当于要求我们在变更集中添加域表生成sql。 It makes sense if it's changing (that's what a change set is for!), but if it's just being initially created, it's conceptually the wrong place to put it.如果它正在更改(这就是更改集的用途!),这是有道理的,但如果它只是最初创建的,那么在概念上放置它是错误的。

Further we can't use the migration plugin because our production QA process does not allow for that approach (it is an external process not tied to the project source so it doesn't make sense to have the change sets maintained in multiple projects).此外,我们不能使用迁移插件,因为我们的生产 QA 流程不允许这种方法(它是一个与项目源无关的外部流程,因此在多个项目中维护更改集是没有意义的)。

Hibernate generates it's schema automatically from the grails domain objects, we have the sql for the views (dependent on the tables from the domain objects) as well as the sql for a few functions used within those views, and we just need to determine where to best have GORM / Hibernate execute said sql in order to generate the appropriate db objects when the schema is generated. Hibernate 从 grails 域对象自动生成它的模式,我们有视图的 sql(依赖于域对象的表)以及这些视图中使用的一些函数的 sql,我们只需要确定在哪里最好让 GORM/Hibernate 执行上述 sql,以便在生成模式时生成适当的 db 对象。

In Grails 2.5.5 I used a custom GrailsAnnotationConfiguration to hook in to the schema generation process.在 Grails 2.5.5 中,我使用了自定义 GrailsAnnotationConfiguration 来挂钩模式生成过程。 This is no longer an option with the use of Hibernate 5 in grails 4+.在 grails 4+ 中使用 Hibernate 5 时,这不再是一个选项。

What is the best way to tie in to the Hibernate Schema generation process in order to add custom db objects like functions and views without relying on the database migration plugin?为了在不依赖数据库迁移插件的情况下添加自定义 db 对象(如函数和视图),绑定到 Hibernate Schema 生成过程的最佳方法是什么?

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.目前(Grails 4.05)的答案似乎是使用数据库迁移插件(如原始问题中所述,这对我来说不合适)或使用自定义 HibernateMappingContextConfiguration 以便通过覆盖buildSessionFactory() 方法。 These objects are documented in the Hibernate documentation, and the config is mentioned in the GORM documentation.这些对象记录在 Hibernate 文档中,配置在 GORM 文档中提到。

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.我无法让 Gorm/Grails 关注 hibernate 配置和映射文件。 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.幸运的是,HibernateMappingContextConfiguration 支持直接添加 AuxiliaryDatabaseObject,通过它您可以有效地注入创建或删除辅助 db 对象所需的任何 SQL,包括函数、视图,甚至任意 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.这对我有用,并且具有以下优点:以这种方式注入的任何代码(自然地)执行到使用模式导出时生成的 ddl,这在数据库管理(包括更改集管理)是外部的时是必需的,具有导出的 ddl 是生成的工件。

Additionally, this lets you keep your dev environment using 'create-drop' for the datasource (so it always starts up from a pristine schema).此外,这使您可以使用数据源的“create-drop”来保持开发环境(因此它始终从原始模式启动)。

for example:例如:

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

and then just configure hibernate to use your custom context config via application.yml:然后只需配置休眠以通过 application.yml 使用您的自定义上下文配置:

hibernate:
    configClass: path.to.MyHibernateMappingContextConfiguration  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM