在hibernate中更新查询

时间:2012-12-13 08:49:35

标签: hibernate

我在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课程也在这里:

@Entity
@Table(name="EXAMRESULTS")
public class ExamResults implements Serializable{

    private int id;
private String title;
private String body1;
private String body2;
private Date resultdate;
private String affiliation;
private int isactive;
private String url;
private String imageurl;
//private CommonsMultipartFile imageurlimg;
private String metatitle;
private String metadescription;
private String metakeywords;
private Date createddate;


    --getters & setters

解决问题的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

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

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

HQL文档提到:

  

除了Java类和属性的名称,查询   不区分大小写。所以SeLeCT与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