所以我有以下實體:按日期刪除行從DB
@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日 而且我試圖刪除日期低於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
爲什麼我不能刪除這樣的行?什麼是正確的方法呢?
你有意使用'createNativeQuery'而不是'createQuery'嗎? – 2014-10-06 09:39:13
什麼是creationTs列的數據類型? – Karthik 2014-10-06 09:40:01
就是這樣。 'createNativeQuery'完成了這個訣竅。謝謝 – SaintLike 2014-10-06 09:44:46