Search code examples
javasqlhibernatehibernate-mapping

Update specific field of table regardless of other fields in hibernate


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();
        hibernateTemplateObj.update(myEntityBean);

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.


Solution

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

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

    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.