我目前正在将Grails 4.0.1应用程序升级到Grails 4.0.11,我的一个域类的联接表遇到了问题。
我的Account
类与我的类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中预期的一样工作,在数据库中创建了多个联接表。然而,当在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
中指定映射来获得创建的数据库表account_success_completion_statuses
和account_no_show_completion_statuses
,但我仍然无法自定义account_partial_completion_statuses
:的表名和列名
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
的数据库表,而不是account_partial_completion_statuses
,它有列account_partial_completion_statuses_id
和completion_status_id
。
如果需要更多上下文,我在应用程序中使用MySQL 5.6.51和库mysql:mysql-connector-java:5.1.48
。
我在joinTable上的Grails文档中找到了解决方案!
联接表在数据库中正确创建,当我指定Account
类时,应用程序运行良好,如下所示:
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
}
}