1

I am working on a project for college that uses GWT, Hibernate and Gilead. Basically for the moment users should be able to add friends and remove them. Also, a user can see if his or her friends are online or not.

My trouble is that when I add a friend that is already related to another friend, I get this error:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value     was already associated with the session: [com.example.client.YFUser#4]

This is my service class for my gwt application:

public class TestServiceImpl extends PersistentRemoteService implements TestService {

My trouble is here with my implementation class of my service in this method, which is called when a user presses the add friend button on the client-side.

    public void addYFUserFriend(String userName){
            //this retrieves the current user
    YFUser user = (YFUser)getSession().getAttribute(SESSION_USER);

    Session session = com.example.server.HibernateUtil.getSessionFactory().getCurrentSession();

    session.beginTransaction();

    YFUser friend = (YFUser) session.createQuery("select u FROM YFUser u where u.username = :username").setParameter("username", userName).uniqueResult();
    System.out.println("user " + friend.getUsername() + " Found");

    user.getFriends().add(friend);

    friend.getBefriended().add(user);
            session.update(user);
            session.update(friend);

    session.getTransaction().commit();
}

A scenario:

user1 adds user2 as a friend. This works fine, Then user3 adds user2 and the exception is thrown.

Any ideas why and where my logic is going wrong?

Update: Ok, so I have changed my code, and I have removed all the getCurrentASession() calls and replaced with openSession() calls which are closed at the appropriate point, now the error I am getting is:

com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract void com.example.client.TestService.addYFUserFriend(java.lang.String)' threw an unexpected exception: org.hibernate.NonUniqueResultException: query did not return a unique result: 3
molleman
  • 2,934
  • 16
  • 61
  • 92
  • What is the scope of your Hibernate Session/where is it managed? In the example given you're using getCurrentSession(). Is there a filter on the front end that's opening a new session? Sharing one never-ending hibernate session across many users is a formula for disaster :) – Affe May 27 '10 at 19:56
  • i do not have a filter on the front end and i dont really know what it is, could give me some insight on how not to share a session.(i dont really know if i am or not). i only open a session to send a user to the client side, which allows them to be logged in, and then opening a session to save teh user when they log out or update something. a headsup on how not to share session would be great? – molleman May 28 '10 at 00:19
  • also i am managing the session with a hibernateUtil class, which is similar to the one in this example , some modifications for use with gilead, http://code.google.com/webtoolkit/articles/using_gwt_with_hibernate.html – molleman May 28 '10 at 00:22
  • From a hibernate perspective, it looks like you're saying getCurrentSession() when you should be saying openSession(), both of your web users appear to be trying to use the same hibernate session. However, I have only a passing familiarity with Gilead, so I can't comment intelligently on how the Session management should work within the context of Gilead being on, sorry I can't be more helpful :( – Affe May 28 '10 at 00:29

1 Answers1

2

It looks to me like you have 3 users with the same user name. As you are using uniqueResult(), you are telling Hibernate that you are expecting only a single value.

Check your database or replace uniqueResult() with List() to see what you get back.

Kango_V
  • 1,720
  • 2
  • 15
  • 11