我要执行以下查询:
@Override
public int deleteOldLogByDate(LocalDateTime created_at) throws Exception {
String hql = "delete from " + Log.class.getName() + " e where e.created_at <= :created_at";
TypedQuery<Log> query = entityManager.createQuery(hql, Log.class).setParameter("created_at", created_at);
int records = query.executeUpdate();
return records;
}
但是我得到了例外:java.lang.IllegalArgumentException: Update/delete queries cannot be typed at
您知道我该如何解决这个问题?
答案 0 :(得分:1)
不能在TypedQuery中使用批量查询(如更新和删除)(如错误所示)。
您必须使用:
@Override
public int deleteOldLogByDate(LocalDateTime created_at) throws Exception {
String hql = "delete from " + Log.class.getName() + " e where e.created_at <= :created_at";
Query query = entityManager.createQuery(hql).setParameter("created_at", created_at);
int records = query.executeUpdate();
return records;
}