-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: saveOrUpdateMerge method?
PostPosted: Fri May 27, 2005 11:43 am 
Beginner
Beginner

Joined: Thu Feb 17, 2005 4:20 pm
Posts: 29
I have the following use case:

When a user object is updated, we have to verify the uniqueness of username. The business rule alows a user to change their username, so we first load the user by username to check that the new username is unique. The updated user object is a detatched object, so when we save the detached object we have to merge it if it is in session from the load. If this is a new record, or if the username has changed, the object is not in the session and we just issue a saveOrUpdate() without a merge.

Here is my saveUser method:

Code:
  public User saveUser( User user )
  {
    SessionImplementor session = (SessionImplementor) HibernateSessionManager.getSession();
    if (user.getId() == null)
    {
      //this is a new user...just save it
      session.save( user );
    }
    else
    {
      final PersistenceContext persistenceContext = session.getPersistenceContext();
      EntityKey key = new EntityKey( user.getId(), session.getEntityPersister( null, user ) , session.getEntityMode()  );

      if ( persistenceContext.containsEntity( key ) )
      {
        user = (User)session.merge( user );
      }
      else
      {
        session.saveOrUpdate( user );
      }
    }
    return user;
  }


Is this a good solution? Is this a case for a saveOrUpdateMerge() method?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.