2012-12-13 139 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

感謝輸入..它現在... –