如何在 Grails 4 域 class 中指定连接 table 的名称和列?

How do you specify a join table's name and columns in a Grails 4 domain class?

我目前正在将 Grails 4.0.1 应用程序升级到 Grails 4.0.11,我 运行 遇到了我的一个域 table 的加入问题 classes.

我的 Account class 与我的 class CompletionStatus:

有多个 hasMany 关系
import grails.compiler.GrailsCompileStatic
import grails.gorm.hibernate.mapping.MappingBuilder

@GrailsCompileStatic
class Account {

    String name

    static hasMany = [successCompletionStatuses: CompletionStatus, noShowCompletionStatuses: CompletionStatus,
            partialSegmentAttendanceCompletionStatuses: CompletionStatus]

    // Have distinct join tables for the CompletionStatus associations
    // https://github.com/grails/grails-core/issues/10811#issuecomment-337931115
    static mapping = MappingBuilder.orm {
        property('successCompletionStatuses') {
            joinTable { name "account_success_completion_statuses" }
        }
        property('noShowCompletionStatuses') {
            joinTable { name "account_no_show_completion_statuses" }
        }
        property('partialSegmentAttendanceCompletionStatuses') {
            joinTable {
                name "account_partial_completion_statuses"
                key {
                    name "account_partial_completion_statuses_id"
                }
                column {
                    name "different_completion_status_id"
                }
            }
        }
    }

    static constraints = {
        name blank: false
    }
}
import grails.compiler.GrailsCompileStatic

@GrailsCompileStatic
class CompletionStatus {

    String courseType
    String status

    static constraints = {
        courseType nullable: true
        status blank: false
    }
}

Account 中的映射在 Grails 4.0.1 中按预期工作,在数据库中创建了多个连接 table。但是,当 运行 Grails 4.0.11 中的应用程序时,我在启动时遇到错误,我最终发现可以通过从 Account:

中删除多个 hasMany 关系来防止该错误
Caused by: java.lang.NullPointerException: null
    at org.hibernate.mapping.Column.getCanonicalName(Column.java:362)
    at org.hibernate.mapping.Table.getColumn(Table.java:233)
    at org.hibernate.mapping.Table.addColumn(Table.java:257)
    at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindSimpleValue(GrailsDomainBinder.java:2938)
    at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindCollectionSecondPass(GrailsDomainBinder.java:450)
    at org.grails.orm.hibernate.cfg.GrailsDomainBinder$GrailsCollectionSecondPass.doSecondPass(GrailsDomainBinder.java:3406)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1661)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
    at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:287)
    at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:86)
    at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
    at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
    at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
    at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
    at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
    at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:212)
    at sun.reflect.GeneratedConstructorAccessor64.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:175)
    ... 123 common frames omitted

我发现我可以像这样通过在 Acccount 中指定映射来获取创建的数据库 tables account_success_completion_statusesaccount_no_show_completion_statuses,但我仍然无法为 account_partial_completion_statuses:

自定义 table 名称和列名称
static mapping = MappingBuilder.orm {
    property('successCompletionStatuses') {
        joinTable "account_success_completion_statuses"
    }
    property('noShowCompletionStatuses') {
        joinTable "account_no_show_completion_statuses"
    }
    property('partialSegmentAttendanceCompletionStatuses') {
        joinTable {
            name "account_partial_completion_statuses"
            key "account_partial_completion_statuses_id"
            // This isn't setting the column name
            column "different_completion_status_id"
            // This isn't setting the column name either
            // column { name "different_completion_status_id" }
        }
    }
}

使用上面的映射,创建了一个名为 account_completion_status 的数据库 table 而不是 account_partial_completion_statuses,它有列 account_partial_completion_statuses_idcompletion_status_id

如果需要更多上下文,我在我的应用程序中使用 MySQL 5.6.51 和库 mysql:mysql-connector-java:5.1.48

我在 Grails documentation on joinTable!

中找到了解决方案

连接表在数据库中正确创建,当我像这样指定 Account class 时应用程序运行正常:

import grails.compiler.GrailsCompileStatic

@GrailsCompileStatic
class Account {

    String name

    static hasMany = [successCompletionStatuses: CompletionStatus,
            noShowCompletionStatuses: CompletionStatus,
            partialSegmentAttendanceCompletionStatuses: CompletionStatus]

    // Have distinct join tables for the CompletionStatus associations
    // https://docs.grails.org/3.1.1/ref/Database%20Mapping/joinTable.html
    static mapping = {
        successCompletionStatuses joinTable: [
                name: 'account_success_completion_statuses']
        noShowCompletionStatuses joinTable: [
                name: 'account_no_show_completion_statuses']
        partialSegmentAttendanceCompletionStatuses joinTable: [
                name: 'account_partial_completion_statuses',
                key: 'account_partial_completion_statuses_id',
                column: 'different_completion_status_id']
    }

    static constraints = {
        name blank: false
    }
}