13

I have tried a lot to update my table using hql but i didn't find the solution , i have searched on internet too, I am new in java and hibernate please help me to find the solution.

my code is written below.

session.getTransaction().begin();
Query query = session.createQuery("update DocDetail set DocName = :docname" + 
    " where Id = :docId");
query.setParameter("docname", "Jack");
query.setParameter("docId", 3);
int result = query.executeUpdate();
session.getTransaction().commit();

but I got the following error.

Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: query must begin with SELECT or FROM: update [update clinic.entity.DocDetail set DocName = :studentName where Id = :studentId]
at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:106)
at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:131)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:51)

5 Answers 5

19

If you are using hibernate, you should try to access entities not tables.
The biggest advantage of hibernate is that it provides you ORM (object relational mapping).
Here is the example how to update an entity with hibernate
(of course corresponding table is also updated).

/* Method to UPDATE salary for an employee */
   public void updateEmployee(Integer EmployeeID, int salary ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Employee employee = 
                    (Employee)session.get(Employee.class, EmployeeID); 
         employee.setSalary( salary );
         session.update(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Why is a transaction required here?
usually u do the updates in the boundaries of transaction. however you may remove transaction related code. if you are sure you dont need it.
15

You are creating a Native(SQL) query using createQuery() method instead of createSQLQuery() method so just change your code as follows

session.getTransaction().begin();
Query query = session.createSQLQuery(
    "update DocDetail set DocName = :docname" + " where Id = :docId");
query.setParameter("docname", "Jack");
query.setParameter("docId", 3);
int result = query.executeUpdate();
session.getTransaction().commit();

read about about this in detail:

Comments

5

To update object without SQL or HQL you can use next code snippet.

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     sess.update(yourObject);
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

Read documentation about update - possible you have to use merge or saveOrUpdate.

2 Comments

createQuery is for creating queries. It's called executeUpdate for a reason.
False, createQuery is not just for selection. You can execute updates and deletes too. In SQL jargon any DML operation is a Query.
3

Here a way of updating data into table using hibernate hql:

Configuration cfg = new Configuration();
cfg.configure("HibernateService/hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

String hql = "UPDATE Userreg SET uname = :uname, uemail = :uemail, uphone = :uphone WHERE uemail = :uemail";

Query query = session.createQuery(hql);
query.setParameter("uname", uname);
query.setParameter("uemail", uemail);
query.setParameter("uphone", uphone);
int rr = query.executeUpdate();

t.commit();

if (rr != 0) {
    return true;
} else {
    return true;
}

3 Comments

Above code for updating data into table using hibernate it was worked properly
your comment should be added in your answer. So that fellow users will have some idea what it is and why it’s there.
You should never, ever, ever string concatenate in a db query.
-1

you can use hibernate session's merge. such as

User user = session.find("1"); 
//get Persistence entity``String userName = user.getUserName(); //  userName = "enzo"
//user.setUserName("leo");
session.merge(user);
// Test entity user's useName
String userNameNew = session.find("1").getUserName; // now userName is "leo"

I hope can help you;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.