HibernateException:命名查询中的错误

时间:2013-05-13 15:51:49

标签: sql hibernate annotations hibernateexception

当运行特定的单元测试时,我得到了例外:

Caused by: org.hibernate.HibernateException: Errors in named queries: UPDATE_NEXT_FIRE_TIME
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:437)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:891)
... 44 more

用于此处定义的命名查询:

@Entity(name="fireTime")
@Table(name="qrtz_triggers")
@NamedQueries({
@NamedQuery(
        name="UPDATE_NEXT_FIRE_TIME",
        query= "update fireTime t set t.next_fire_time = :epochTime where t.trigger_name = 'CalculationTrigger'")
})
public class JpaFireTimeUpdaterImpl implements FireTimeUpdater {

@Id
@Column(name="next_fire_time", insertable=true, updatable=true)
private long epochTime;

public JpaFireTimeUpdaterImpl() {}

public JpaFireTimeUpdaterImpl(final long epochTime) {
    this.epochTime = epochTime;
}

@Override
public long getEpochTime() {
    return this.epochTime;
}

public void setEpochTime(final long epochTime) {
    this.epochTime = epochTime;
}

}

在我尽可能深的调试之后,我发现在QueryTranslatorImpl中的w.statement(hqlAst)中发生了异常:

    private HqlSqlWalker analyze(HqlParser parser, String collectionRole) throws QueryException, RecognitionException {
    HqlSqlWalker w = new HqlSqlWalker( this, factory, parser, tokenReplacements, collectionRole );
    AST hqlAst = parser.getAST();

    // Transform the tree.
    w.statement( hqlAst );

    if ( AST_LOG.isDebugEnabled() ) {
        ASTPrinter printer = new ASTPrinter( SqlTokenTypes.class );
        AST_LOG.debug( printer.showAsString( w.getAST(), "--- SQL AST ---" ) );
    }

    w.getParseErrorHandler().throwQueryException();

    return w;
}

我的查询或注释有问题吗?

1 个答案:

答案 0 :(得分:3)

NamedQuery应该用JPQL编写,但查询似乎混合了持久属性的名称和数据库列的名称。数据库列的名称不能在JPQL中使用。

在这种情况下,应使用持久属性next_fire_time的{​​{1}}名称。另外epochTime看起来更像是数据库列的名称而不是持久属性的名称,但它似乎根本不会映射到当前类中。映射后,查询如下:

trigger_name

如果首选SQL查询,则应使用@NamedNativeQuery代替。

作为旁注,JPA 2.0规范不鼓励更改主键:

  

应用程序不得更改主键的值[10]。该   如果发生这种情况,行为是不确定的。[11]

通常,实体不知道通过JPQL查询所做的更改。当尝试刷新不再存在的实体时(因为主键已更改),这会变得特别有趣。

此外,命名有点令人困惑:

  1. 类的名称看起来更像是服务类的名称 而不是实体的名称。
  2. 较低的实体的起始名称 案件信件是相当罕见的风格。
  3. 实体名称,名称 表格和班级名称不太匹配。
相关问题