views:

243

answers:

1

Hello Guys,

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]

i have a service class

public class TestServiceImpl extends PersistentRemoteService implements TestService {

this is my service class for my gwt application.

my toruble is here with my implmentation class of my serivce in this method that is called when a user presses 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 scenerio :

user1 adds user2 as a friend. this works fine. then user3 adds user2 and the exeception is thrown.

any ideas why and where my logic is going wrong

Ok so i have changed my code, and i have removed all the getCurrentASession() calls and replaced with openSession() call which are closed at the appropiate 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
+1  A: 

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