2

I am fully testing an entity on my unit test, and almost everything worked so far: create, update, list. However, when I try to delete a record, it is not getting deleted. Here is the code I am using:

  public void delete (Integer id) {
    // This doesnt work even though I know user is set and id is not null
    User user = find(id);
    getSession().delete(user);
    // This will work
    // Query query = getSession().createSQLQuery("DELETE FROM users WHERE id = " + id);
    // query.executeUpdate();
  }    

    private Session getSession() {
      if (session == null) {        
        try {
            session = SessionFactoryUtils.getSession(sessionFactory, Boolean.TRUE);
          TransactionSynchronizationManager.bindResource(session.getSessionFactory(), new SessionHolder(session));            
        } catch (Exception e) {
            session = SessionFactoryUtils.getSession(sessionFactory, Boolean.FALSE);          
        }
      }
      return session;
    }

If I execute the query directly it works but using the delete() method doesnt. I think it may be related to committing the transaction but I already tried something like that and no luck. Any ideas?

1 Answer 1

1

I found the problem with this one.

First, find() method was evicting my user model, and probably taking it out of the session.

After delete(), I also needed to session.flush()

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, what do you mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.