1

次のように domain.executeUpdate を使用して、テーブルから 1 か月前のレコードを削除しようとしています。

Bugrerun.executeUpdate("delete Bugrerun b where b.complete = 1 and b.date 
< date_sub(curdate(), INTERVAL 1 MONTH) ")

クエリ内で MySQL 日付関数を使用しようとしています。

しかし、これはエラーで失敗します

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 1 near line 1
, column 97 

executeUpdate ステートメント内で My SQL 日時関数を使用するにはどうすればよいですか

このテーブルには大量のデータがあるため、個々のレコードを取得して削除することはできません。

4

3 に答える 3

2

以下のクエリを試すことができます。HQL 関数が MySQL 方言でサポートされているかどうかを検証する必要があるだけです。

Bugrerun.executeUpdate("delete Bugrerun b \ 
                        where b.complete = 1 \
                        and month(current_date()) > month(b.date) \
                        or year(current_date()) > year(b.date)")
于 2013-08-16T14:26:25.063 に答える
1

すべての mysql 関数が使用できるわけではありません。hibernate (および grails) で使用されている MySQLDialect を見て、利用できる機能を確認できます: http://grepcode.com/file/repository.springsource.com/org.hibernate/com.springsource。 org.hibernate/3.3.1/org/hibernate/dialect/MySQLDialect.java#MySQLDialect

必要に応じて、Groovy SQL を使用して、HQL ステートメントの代わりに SQL ステートメントを実行してみてください。その場合、コントローラーまたはサービスで dataSource 属性を宣言して、DataSource が注入されるようにする必要があります。

class MyController {
    DataSource dataSource

    def execSql(){
            def sql = new Sql(dataSource)
            sql.execute("delete from bugrerun where complete = 1 and date < date_sub(curdate(), INTERVAL 1 MONTH) ")
            render "done"
    }
}
于 2013-08-16T15:40:05.993 に答える