2012-12-13 53 views
0

我在hibernate上更新hql查询时遇到问题。更新休眠中的查询

这里是我下面的类:

--session configuration 
    ExamResults examResults = new ExamResults(); 
    examResults.setTitle("cbse board jee"); 
    examResults.setId(2); 
    String hql = "UPDATE EXAMRESULTS SET TITLE=:TITLE WHERE ID=:ID"; 
    Query query = session.createQuery(hql); 
    query.setParameter("TITLE", examResults.getTitle()); 
    query.setParameter("ID", examResults.getId()); 
    int result = query.executeUpdate(); 
    System.out.println("Rows Effected=>"+result); 


    session.save(examResults); 

    tx.commit(); 

    } 
} 

在运行类,我得到以下异常:

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: EXAMRESULTS is not mapped [UPDATE EXAMRESULTS SET TITLE=:TITLE WHERE ID=:ID] 
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) 
    ... 

而且我bean类是在这里:

​​

什么应该以正确的方式解决问题?

回答

1

而不是EXAMRESULTS尝试使用ExamResults即您的实体的类名称。您需要在HQL查询中使用类名,而不是表名。因此,更新查询:

UPDATE ExamResults e SET e.TITLE=:TITLE WHERE e.ID=:ID 

的HQL文档中提到:

除了Java类与属性的名称外,查询 不区分大小写。因此SeLeCT与sELEct相同,但与org.hibernate.eg.FOO和org.hibernate.eg.Foo是 不同,foo.barSet和foo.BARSET也是如此。

文档:http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#d5e2551

+0

感谢输入..它现在... –