spring pro
spring pro

Reputation: 107

updation query in hibernate

I am facing the problem on update hql query on hibernate.

Here is my following class:

    --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();

   }
}

On running the class, i am getting following exception:

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)
    ...

Also my bean class is here:

@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

What should be correct approach to resolve problem?

Upvotes: 0

Views: 74

Answers (1)

Viral Patel
Viral Patel

Reputation: 8601

Instead of EXAMRESULTS try using ExamResults i.e. the class name of your entity. You need to use class name in your HQL queries instead of table name. So update the query to:

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

The HQL documentation mentions:

With the exception of names of Java classes and properties, queries are case-insensitive. So SeLeCT is the same as sELEct is the same as SELECT, but org.hibernate.eg.FOO and org.hibernate.eg.Foo are different, as are foo.barSet and foo.BARSET.

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

Upvotes: 1

Related Questions