所以我有以下实体:

@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 */


我要插入几行从2014年1月10日到2014年6月10日之间的随机日期
我正在尝试删除日期小于2014年5月10日(昨天)的所有行。
当查询数据库时,我应该只有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


为什么不能删除这样的行?正确的方法是什么?

最佳答案

DELETE FROM TrxIdList where (trunc(creationTs) < trunc(SYSDATE-1))


...看起来不像Java持久性查询。

我怀疑您是要使用createNativeQuery创建本地SQL查询,而不是使用createQuery创建持久性查询。

09-10 08:23
查看更多