user3205372
user3205372

Reputation: 163

how to update the table in java using hibernate without using hql and sql

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)

Upvotes: 13

Views: 86367

Answers (5)

Divers
Divers

Reputation: 9569

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.

Upvotes: 5

Roopa Veriton
Roopa Veriton

Reputation: 31

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;
}

Upvotes: 3

faisalbhagat
faisalbhagat

Reputation: 2150

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

Upvotes: 19

罗智博
罗智博

Reputation: 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;

Upvotes: -1

Ashish Jagtap
Ashish Jagtap

Reputation: 2819

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:

Upvotes: 15

Related Questions