How to update table in java using hibernate without using hql and sql

I tried a lot to update my table with hql, but I did not find a solution, I also searched the Internet, I am new to java and hibernate, help me find a 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) 
+10
source share
5 answers

If you use hibernation, you should try to access objects without tables.
The biggest advantage of hibernation is that it provides you with ORM (relational object mapping).
The following is an example of updating an entity using sleep mode (of course, the 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(); } } 
+18
source

You create your own (SQL) query using createQuery() instead of the 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 more about this:

+14
source

To update an object without SQL or HQL, you can use the following code fragment.

  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 the update documentation - you may have to use merge or saveOrUpdate .

+4
source

Here is a way to update data in a 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; } 
+2
source

you can use sleep 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 useName String userNameNew = session.find("1").getUserName; // now userName is "leo" 

I hope that can help you;

-1
source

All Articles