Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 766
user3205372 Avatar asked Jan 21 '14 05:01

user3205372


People also ask

Which is better SQL or HQL?

The following are some of the reasons why HQL is preferred over SQL: Provides full support for relational operations. It is possible to represent SQL Queries in the form of objects in HQL which uses classes and properties instead of tables and columns. Return results as objects.

How update method works in hibernate?

update() method updates the entity for persistence using the identifier of detached object or new instance of entity created with existing identifier. If the object is already in the session with the same identifier, then it throws exception.

Is HQL same as SQL?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.

How do you update an object in hibernate?

We can update an object in hibernate by calling the update() method, provided by the org. hibernate. Session. Though the update() method is used to update an object, there are two different ways to use update() method.


2 Answers

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(); 
      }
   }
like image 169
faisalbhagat Avatar answered Oct 06 '22 00:10

faisalbhagat


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;
}
like image 43
Roopa Veriton Avatar answered Oct 05 '22 23:10

Roopa Veriton