所以我有以下实体:
@Entity
@Customizer(GridCacheCustomizer.class)
public class TrxIdList implements Serializable {
private static final long serialVersionUID = 9086928639117607157L;
@Id
private String Id;
@Version
protected int version;
private Timestamp creationTs;
...
/*geters and setters go here */
我正在插入几行,随机日期为1-10-2014至06-10-2014 我正在尝试删除日期低于05-10-2014(昨天)的所有行。 在查询数据库时,我应该只有05和06日期。 我正在尝试删除这样的行:
int deletedCount = em.createQuery("DELETE FROM TrxIdList where (trunc(creationTs) < trunc(SYSDATE-1))").executeUpdate();
但是,我一直收到以下错误:
Exception in thread "Thread-3" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [DELETE FROM TrxIdList where (trunc(creationTs) < trunc(SYSDATE-1))].
[28, 68] The expression is not a valid conditional expression.
[55, 62] The left expression is not an arithmetic expression.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
at main.TrxJanitor.run(TrxJanitor.java:43)
at java.lang.Thread.run(Thread.java:744)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [DELETE FROM TrxIdList where (trunc(creationTs) < trunc(SYSDATE-1))].
[28, 68] The expression is not a valid conditional expression.
[55, 62] The left expression is not an arithmetic expression.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
... 2 more
为什么我不能删除这样的行?这样做的正确方法是什么?
答案 0 :(得分:2)
DELETE FROM TrxIdList where (trunc(creationTs) < trunc(SYSDATE-1))
...看起来不像Java持久性查询。
我怀疑您使用createNativeQuery创建本机SQL查询,而不是使用createQuery创建持久性查询。
答案 1 :(得分:0)
我猜您的查询可能存在一些问题。请查看以下代码段。
Query qry = session.createQuery("DELETE FROM TrxIdList where creationTs < trunc(SYSDATE-1)");
int deletedRecords = qry.executeUpdate();