Question

With the following method I am trying to update a column named accesstoken. There is no exception,there is no error but the table's column doesn't get updated. What could be the reason ? Am I leaving something ?

public void updateAccessToken(int id , String accessToken) {

    try {
        Session session = new HVR().getSession();
        System.out.println("ID :--> " + id);
        String hql = "UPDATE UCred set accessToken ='" + accessToken + "'" + " WHERE id ='" + id + "'";
        Query query = session.createQuery(hql);
        query.executeUpdate();
        session.close();
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

Note: Initially accesstoken is set to null.

Was it helpful?

Solution

If you manage your own transaction (i.e. not managed by containers like Spring, JTA), then you need to begin and end the transaction.

Using

session.getTransaction().begin();

and

session.getTransaction().commit(); 

Then your code would be like this,

public void updateAccessToken(int id , String accessToken) {

    try {
        Session session = new HVR().getSession();

session.getTransaction().begin();
        System.out.println("ID :--> " + id);
        String hql = "UPDATE UCred set accessToken ='" + accessToken + "'" + " WHERE id ='" + id + "'";
        Query query = session.createQuery(hql);
        query.executeUpdate();

session.getTransaction().commit();

        session.close();
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

OTHER TIPS

IS it because you're enclosing id in quotes, e.g.

" WHERE id ='" + id + "'"

?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top