hibernate - 在resource.groovy中注册了多个Hibernate事件监听器

标签 hibernate grails

在我们的应用程序中,我们有两个休眠事件侦听器,一个用于审核日志记录,另一个用于在Solr服务器中建立索引。

但是到目前为止,我们面临的问题只是一个。
请在下面找到配置详细信息:

程式码片段

A] Inside  AuditLoggingProjPlugin.groovy:
def loadAfter = ['core','hibernate']

        def doWithSpring = {
          if (manager?.hasGrailsPlugin("hibernate")) {
            auditLogListener(AuditLogListener) {
                grailsApplication = ref('grailsApplication')
                sessionFactory   = sessionFactory
                verbose          = application.config?.auditLog?.verbose?:false
                transactional    = application.config?.auditLog?.transactional?:false
                sessionAttribute = application.config?.auditLog?.sessionAttribute?:""
                actorKey         = application.config?.auditLog?.actorKey?:""
                logIds           = application.config?.auditLog?.logIds?:false
                replacementPatterns = application.config?.auditLog?.replacementPatterns?:null
                propertyMask        = application.config?.auditLog?.propertyMask?:""
            }
          }
        }

        def doWithApplicationContext = { applicationContext ->
          // pulls in the bean to inject and init
          AuditLogListener listener = applicationContext.getBean("auditLogListener")
          // allows to configure the Actor name Closure in the config
          listener.setActorClosure(application.config?.auditLog?.actorClosure?:AuditLogListenerUtil.actorDefaultGetter )
          listener.init()
                // allows user to over-ride the maximum length the value stored by the audit logger.
          if(application.config?.auditLog?.TRUNCATE_LENGTH) {
            listener.truncateLength = new Long(application.config?.auditLog?.TRUNCATE_LENGTH)
          }
        }
B] Inside  SolrIndexProjPlugin.groovy:
def doWithSpring = {
        // TODO Implement runtime spring config (optional)
        if (manager?.hasGrailsPlugin("hibernate")) {
            searchPersistenceListener(SearchPersistenceListener) {
                solrIndexService =  ref("solrIndexService")
            }
          }
        //start of search indexing listener
        //this might need to be commented out if other plugins are not up to date
       /* searchPersistenceListener(com.xms.core.search.SearchPersistenceListener){
            solrIndexService =  ref("solrIndexService")
        }

        hibernateEventListeners(org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners) {
          listenerMap = ['post-commit-insert': searchPersistenceListener,
                         'post-commit-update': searchPersistenceListener,
                         'post-commit-delete': searchPersistenceListener]
        }*/
        //end of search indexing listener
    }

    def doWithDynamicMethods = { ctx ->
        // TODO Implement registering dynamic methods to classes (optional)
    }

    def doWithApplicationContext = { applicationContext ->
        // TODO Implement post initialization spring config (optional)

        def listeners = applicationContext.sessionFactory.eventListeners
        SearchPersistenceListener listener = applicationContext.getBean("searchPersistenceListener") 
        ['postCommitInsert', 'postCommitUpdate', 'postCommitDelete'].each({
           addEventTypeListener(listeners, listener, it)
        })
    }

    def addEventTypeListener(listeners, listener, type) {
        def typeProperty = "${type}EventListeners"
        def typeListeners = listeners."${typeProperty}"

        def expandedTypeListeners = new Object[typeListeners.length + 1]
        System.arraycopy(typeListeners, 0, expandedTypeListeners, 0, typeListeners.length)
        expandedTypeListeners[-1] = listener

        listeners."${typeProperty}" = expandedTypeListeners
    }

我强烈认为我在项目插件的配置中缺少了某些内容,而这些内容完全丢失了。

强烈需要帮助。

此致,MB

最佳答案

不好的是,我没有正确注册侦听器:
添加了以下代码片段:

['postCommitInsert','postCommitUpdate', 'postCommitDelete'].each({
       addEventTypeListener(listeners, listener, it)
    })
}

def addEventTypeListener(listeners, listener, type) {
    def typeProperty = "${type}EventListeners"
    def typeListeners = listeners."${typeProperty}"

    def expandedTypeListeners = new Object[typeListeners.length + 1]
    System.arraycopy(typeListeners, 0, expandedTypeListeners, 0, typeListeners.length)
    expandedTypeListeners[-1] = listener
    listeners."${typeProperty}" = expandedTypeListeners
}

关于hibernate - 在resource.groovy中注册了多个Hibernate事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24629621/

相关文章:

java - 如何在生成的规范元数据类中抑制原始类型警告?

grails - Geb + cucumber : Restart Browser Between Scenarios

html - 当我尝试渲染pdf文件时css在渲染插件中不起作用

Grails promise 丢失数据 - 似乎不等待对方完成

grails - Grails 3脚手架问题

java - 从.sql加载模式以在Spring Boot中创建orm映射

java - 如何使用 Ajax 和 Struts2 将表单数据发送到 ActionSupport

grails - 分页重置行的复选框

java - 如何将 Hibernate "createAlias"子限制添加到父实体限制

json - 当我尝试在 Spring Boot 应用程序中将自定义错误传递给客户端时发生异常