Grails 平台核心事件总线引发DataIntegrationException

Grails 平台核心事件总线引发DataIntegrationException,grails,Grails,我正在使用插件平台内核和事件总线模块。我需要在数据库中添加一些域,但我强制同步测试,但每次我尝试保存()时,都会抛出de exception DataIntegrityViolation。这就是域结构: class Alocacao { static belongsTo = [tipoUnidade: TipoUnidade] //Anothers attrs and constraints } class TipoUnidade { static belongs

我正在使用插件平台内核和事件总线模块。我需要在数据库中添加一些域,但我强制同步测试,但每次我尝试保存()时,都会抛出de exception DataIntegrityViolation。这就是域结构:

  class Alocacao {
    static belongsTo = [tipoUnidade: TipoUnidade]
    //Anothers attrs and constraints
}

class TipoUnidade {

    static belongsTo = [ estabelecimento : Estabelecimento];    

    static hasMany = [alocacoes: Alocacao];

    List<Alocacao> alocacoes

    //Constraints and fields...

}
在我看来:

class AlocacaoService {

@Listener(topic="criarAlocacaoQuartoEvent", namespace="app")
def defaultAlocacaoCreator(EventMessage<TipoUnidade> message) {
   Canal canalSist = Canal.findByNomeIlike("%manual%")
   TipoUnidade quarto = TipoUnidade.get(message.data.id)
   def alocacaoSistema = new Alocacao(exclusivo: 0, nExclusivo: 0, data: tmp.toDate(), canal: canalSist, tipoUnidade: quarto).save(failOnError: true) //EXCEPTION!!
}
}
[编辑]

我创建这个来说明我的问题,同样的错误也会发生

[解决方案] 我用belongsTo类中的addTo*attrs解决了这个问题。但我不知道为什么这样做有效。

核心问题是

MySQLIntegrityConstraintViolationException: Column 'tipo_unidade_id' cannot be null
该列是
tipoundidade
变量的外键(camel case
tipoundidade
变为
tipo_unidade
作为列名,并且添加了
\u id
后缀,因为它是一个FK),这意味着那里的值为null,这意味着
tipoundidade.get(message.data.id)
返回空值。如果
message.data.id
为null(可能是因为
message.data
不是null但尚未保存?),或者没有该id的记录,则会发生这种情况

由于您使用的是异步代码,我猜这是一个计时问题,
message.data
值尚未保存,但您正在尝试根据插入的值加载一个值


检查值,并考虑打开SQL日志,以便查看项目的插入和检索顺序。

< P>基于Burt的响应,我做了一个解决方案,并且出于某些原因,使用Addot*解决了问题。

TipoUnidade quarto = TipoUnidade.get(message.data.id)
quarto.addToAlocacoes(new Alocacao(exclusivo: 0, nExclusivo: 0, data: tmp.toDate(), canal: canalSist))
quarto.save(failOnError: true)

Thx.

我对EventBus也有同样的问题。问题是EventBus与线程一起工作。我想您也使用事务处理。这两个对象都是在两个不同的线程中创建的,hibernate无法保存其中一个,因为另一个线程不可用。

Thx回复Burt。tipoundidade.get(message.data.id)返回一个值,sql日志确认tipoundidadeInstance在数据库中持久化。我不明白nullable的意思。SQL日志:Hibernate:插入tipo_unidade(…)。我尝试了使用newtransaction和whitSession的方法,但得到了相同的结果。
MySQLIntegrityConstraintViolationException: Column 'tipo_unidade_id' cannot be null
TipoUnidade quarto = TipoUnidade.get(message.data.id)
quarto.addToAlocacoes(new Alocacao(exclusivo: 0, nExclusivo: 0, data: tmp.toDate(), canal: canalSist))
quarto.save(failOnError: true)