当我从表中删除所有数据时,我收到一个奇怪的异常。自从我将Hibernate版本从3.x.x更新到4.2.6以来发生了异常。
我调用的Dao方法从表中删除数据:
@Override
public void deleteAll()
{
EntityManager em = getEntityManager();
em.createQuery( "DELETE Document" ).executeUpdate();
}
堆栈跟踪:
12:46:03,570 ERROR ErrorCounter:50 - <AST>:0:0: unexpected end of subtree
<AST>:0:0: unexpected end of subtree
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.whereClauseExpr(SqlGeneratorBase.java:1378)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.whereClause(SqlGeneratorBase.java:1272)
at org.hibernate.hql.internal.ast.exec.DeleteExecutor.<init>(DeleteExecutor.java:72)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.buildAppropriateStatementExecutor(QueryTranslatorImpl.java:535)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:201)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
at sun.reflect.GeneratedMethodAccessor800.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
实体:
@Entity
@Table( name = "document" )
public class Document implements Serializable
{
@Override
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Column( name = "document_id" )
private int id;
private String filename;
@Temporal( TemporalType.TIMESTAMP )
@Column( name = "file_modified_time" )
private Date fileModifiedTime;
@ManyToOne( fetch = FetchType.LAZY )
@JoinColumn( name = "attached_to_Task_fk" )
private Task attachedToTaskFk;
//Getter/Setter
}
当我在删除查询中添加where子句时,不会发生异常。
em.createQuery( "DELETE Document d WHERE d.id > 0" ).executeUpdate();
但我不认为Hibernate会这样做。
我已经检查了以下主题:
有没有人知道如何解决这个问题?
答案 0 :(得分:1)
这似乎是Hibernate bug,已在4.2.7和4.3.0.Beta5中修复。 DeleteExecutor以前需要Where子句。
如果您还没准备好升级到4.2.7,可以在GitHub上找到patch。