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