문제

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