سؤال

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.

هل كانت مفيدة؟

المحلول

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();
    }
}

نصائح أخرى

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

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

?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top