在gorm中创建查询删除

时间:2018-12-12 12:04:15

标签: grails gorm

早上好,我需要在Grails Hibernate(Gorm)中对SQL服务器执行以下查询。 我不明白如何执行查询的顶部

("delete CORE_NOTIFICATIONS where id in (select top 1000 ID form CORE_EVENT where date <10)")

在gorm中,我是这样做的,但是它给出了错误

query = "delete CoreNotifications CN where CN.event_id in (select top? ID from CoreEvent CE where CE.date <?)"
res = CoreNotifications.executeUpdate (query, params)

我怎么了?

1 个答案:

答案 0 :(得分:0)

根据我的经验,您可能会使用GORM删除感兴趣的记录。以下是我以前对项目执行的代码片段。正如您在下面的评论中提到的那样,建议使用并行性,因为该表包含300万条记录。

Date specifiedDate = Date.parse("yyyy-MM-dd hh:mm:ss", "2010-04-03 1:23:45") // pass your date here by extrating from params
def result = CoreEvent.where { date < specifiedDate }
List<Integer> iDs = result.list(sort: "date").collect { it.id }

GparsPool.withPool {

   iDs.eachParallel {
      // we need to wrap inside a single transaction for parallelism
      CoreNotifications.withTransaction {
         CoreNotifications.where { id == it }.deleteAll()
      }
   }
}

我希望这段代码可以帮助您按预期解决问题。