org.hibernate.NonUniqueObjectException Within GWT application using hibernate through gilead

Posted by molleman on Stack Overflow See other posts from Stack Overflow or by molleman
Published on 2010-05-27T19:32:36Z Indexed on 2010/05/28 12:11 UTC
Read the original article Hit count: 359

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about web-development