Update specific field of table regardless of other fields in hibernate

Go To StackoverFlow.com


I have table having lot of information. I am not loading all the information while updating this table from view page.

Below is my code to update the table using hibernate

HibernateTemplate hibernateTemplateObj = getHibernateTemplate();

Above one was updating the fields but the fields which are not visible on my view page will be updated with blank.

So, I used below one...

HibernateTemplate hibernateTemplateObj = getHibernateTemplate();
        hibernateTemplateObj.update("myRequiredField", myEntityBean);

As per documentation here it says if I use update("string",entity) it will update that fields. But it's not working as same.

Can any one please suggest something.

I found some posts.

post1 post2

It says, I need to create queries. But I don't know how to create queries from it? If any one have any idea.. Please help. I think it's known issue from those posts.

2012-04-04 17:19
by Ketan


You can create a query with the Session object like this.

    SessionFactory factory= HibernateUtil.getSessionFactory();  
    session = factory.openSession();  
    Query query = session.createQuery("Select c.productList from Cart c Where c.cartId=:cartId"); 
    query.setParameter("cartId", cart.getCartId());
    List productList=(List) query.list();

While you know what kind of object should return from query you can cast it and use it. You can write native query or hibernate query but I think hibernate query is easier.

2012-04-05 06:31
by mbaydar
Thanks mbaydar, I am not much familiar with hibernate. So, I have a question. Can we use session.createQuery for DML queryies for Update, Delete queries - Ketan 2012-04-05 06:36
Actually hibernate creating many queries for you and what you have to do is just calling hibernates methods. For example if you want to delete object you can call session.delete(object) and hibernate creates query and delete object from database. Like this you can save or update objects. But if you want to do it yourself you can do it just like in my example - mbaydar 2012-04-05 06:41
Thanks mbaydar. I used the same code with my update query and that works - Ketan 2012-04-05 13:11