批量删除关闭参照完整性的域对象

时间:2014-08-09 08:04:42

标签: grails

我正在尝试从我的数据库中批量删除,我禁用了参照完整性。

def dc = grailsApplication.getArtefacts("Domain")*.clazz    
sessionFactory.currentSession.flush()
def db = new Sql(dataSource)
db.withBatch { stmt ->
    stmt.addBatch("SET FOREIGN_KEY_CHECKS = 0")
    dc.each {
        log.info(it.name)
        //if it is a domain a baseDomainClass
        if(it in tao.BaseDomainClass.class){
            //def tableName = GrailsDomainBinder.getMapping(it.getClass()).table.name
            log.info("in "+it.name)
            def tableName = GrailsDomainBinder.getMapping(it.getClass()).table.name
            stmt.addBatch("delete from table " + table_name+" where entity = '"+e+"'")
        }
    }
    stmt.addBatch("SET FOREIGN_KEY_CHECKS = 1")
}
sessionFactory.currentSession.clear()

这是我得到的例外:

Entity= tao.auth.Entity : 2
2014-08-09 10:01:07,859 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver  -     MissingMethodException occurred when processing request: [GET] /logging/deleteDomainObjectsByEntity - parameters:
entity: thomas
No signature of method: static     org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.getMapping() is applicable for argument types: (java.lang.Class) values: [class java.lang.Class]
Possible solutions: getMapping(java.lang.Class), getMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass). Stacktrace follows:
groovy.lang.MissingMethodException: No signature of method: static     org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.getMapping() is applicable for argument types: (java.lang.Class) values: [class java.lang.Class]
Possible solutions: getMapping(java.lang.Class), getMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass)
at     tao.logg.LoggingController$_deleteDomainObjectsByEntity_closure5_closure9.doCall(LoggingController.groovy:262)
at tao.logg.LoggingController$_deleteDomainObjectsByEntity_closure5.doCall(LoggingController.groovy:257)
at tao.logg.LoggingController.deleteDomainObjectsByEntity(LoggingController.groovy:255)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

基本上我需要的是从域对象中获取表名。

任何帮助高度赞赏

提前致谢。

1 个答案:

答案 0 :(得分:0)

GrailsDomainBinder.getMapping(it.clazz)不再是静态的(截至2.3我相信)。

而只是创建一个新实例:

new GrailsDomainBinder().getMapping(it.clazz)

另外,我认为您可能希望使用it.clazz而不是it.class(我可能错了,但我相信前者会为您提供实际的基础域类。)