0

I am using Hibernate with JPA annotated entities and the next code which I expect to update "raw_text" field of an entity doesn't change it. How to achieve the expected behavior?

@Override
public void updatePageWithText(String pageName, String rawText) {
     Session session = HibernateUtils.getInstance().openSession();
     String hql = "FROM WikiPage M WHERE M.name = :name";
     Query query = session.createQuery(hql);
     query.setParameter("name",pageName);

     WikiPage res = null;
     try {
         res = (WikiPage) query.list().get(0);
         res.setRawText(rawText);
         session.update(res);
         session.flush();
     }
     catch (IndexOutOfBoundsException e) {}
     finally {
         session.close();
     }

}
2
  • AFAIK you should open a transaction and commit it before closing the session. Commented Oct 15, 2014 at 0:08
  • 1
    How do you know that you're not getting an IndexOutOfBoundsException? Commented Oct 15, 2014 at 0:15

3 Answers 3

1

Try following:

@Override
public void updatePageWithText(String pageName, String rawText) {
     Session session = HibernateUtils.getInstance().openSession();
     Transaction transaction=session.beginTransaction();
     String hql = "FROM WikiPage M WHERE M.name = :name";
     Query query = session.createQuery(hql);
     query.setParameter("name",pageName);

     WikiPage res = null;
     try {
         res = (WikiPage) query.list().get(0);
         res.setRawText(rawText);
         transaction.commit();
     }
     catch (IndexOutOfBoundsException e) {
         transaction.rollback();
     }
     finally {
         session.close();
     }

}

Note : When you get WikiPage from query, it's still an attached entity so you do not need to fire session.update(), committing the transaction would do the trick.

Sign up to request clarification or add additional context in comments.

Comments

1

The update must be inside of a transaction, which must be commited before the session is closed (or rolled back in the case of an error). Try it with the following code:

    @Override
public void updatePageWithText(String pageName, String rawText) {
    Session session = null;
    Transaction tx = null;

    WikiPage res = null;

    try {
        session = HibernateUtils.getInstance().openSession();
        tx = session.beginTransaction();

        String hql = "FROM WikiPage M WHERE M.name = :name";
        Query query = session.createQuery(hql);
        query.setParameter("name",pageName);
        res = (WikiPage) query.list().get(0);
        res.setRawText(rawText);
        session.update(res);

        tx.commit;
    }
    catch (IndexOutOfBoundsException e) {}
    catch (Exception ex) {
        if (tx!=null) {
            tx.rollback();
        }
        throw ex;
    }
    finally {
        session.close();
    }
}

Under the following link you can find more about transaction handling in hibernate (in the case of a non-managed context which is probably your case): https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html#transactions-demarcation-nonmanaged

Comments

1

u can also user annotation @query with @Modifying to define interface in repository.

@Modifying
@Query(" UPDATE WikiPage w set name = :name ")
public void updateName(@Param("name")String name);

more details in Jpa Repsitory(http://docs.spring.io/spring-data/jpa/docs/1.6.4.RELEASE/reference/html/jpa.repositories.html), chapter 2.3.7 Modifying queries

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.