質問

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