-->
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.  [ 4 posts ] 
Author Message
 Post subject: Duplicate rows with session.update.
PostPosted: Tue Feb 28, 2006 6:48 am 
Newbie

Joined: Wed Feb 01, 2006 10:33 am
Posts: 15
Hi,

Im trying to update a user object using postgreSQL with hibernate.

Ive mapped users to the database with userID as primary key. UserID is set to increment in my hbm file for users.

To edit users I use a JSP page. The edited user object is supposed to replace the existing one whilst retaining the primary key.

Thus far Ive tried this: session.update(user).

This results in two rows. One row with the original PK and edited values in addition to a new PK value with the old object values.

How can I solve this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 7:40 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Paste the code where you update the user (all the code between session.load and session.save).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 8:11 am 
Newbie

Joined: Wed Feb 01, 2006 10:33 am
Posts: 15
method in handleUsersController

Code:
public boolean saveEditedUser( Users user ){
      
      try {
         //  Get session from session factory
         Session session = HibernateSessionFactory.currentSession();
         
         //  Starting a new transaction before interacting with the database
         Transaction transaction = session.beginTransaction();
         
            //  Tell hibernate to save to database, gets the object ID in return
         session.saveOrUpdate( user );
         
         //  Commit to save changes
         transaction.commit();
         
         //  Closing session
         HibernateSessionFactory.closeSession();
         
         return true;


method in JUnit testcase

Code:
public void testSaveEditedUser(){
   Users user = new Users();
   
   user.setUserID( new Integer( 483) );
   user.setLastName( "saveOrUpdate" );
   user.setFirstName( "test" );
   user.setUserName( "test" );
   user.setUserPass( "test" );
   user.setGroupID( 1 );
   // group id m? hentes inn her fra liste "gruppe"
   user.setAddress( "test" );
   user.setHomePhone( "test" );
   user.setCellPhone(  "test"  );
   user.setEmail( "test" );
   user.setPosition( "test" );
   user.setLanguage("no"); // m? hentes inn fra brukerpreferanse session.getParameter("language");
   user.setPostNo( 55 );
   user.setPostRegion( "test" );
   user.setWorkPhone( "test" );
   user.setIsActive( 1 );
   user.setIsDeleted( 0 );
   
   // Send the user to be created in database, returns PrimaryKey of user
   boolean userSaved = handleUsersController.saveEditedUser( user );
   
   // Checks if the method returned true i.e was succesfully executed.
   assertEquals( true, userSaved );
   
}
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 9:31 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

1. You are not updating the user, you're jsut creating a new one.

2. You should have a method like:

public User loadUser(Serializable id){

try {
// Get session from session factory
Session session = HibernateSessionFactory.currentSession();

// Starting a new transaction before interacting with the database
//Transaction transaction = session.beginTransaction();

// Tell hibernate to load the user
User user = (User)session.load(User.class, id);
// also perform check against null & other stuff here


// Commit to save changes
//transaction.commit();

// Closing session
HibernateSessionFactory.closeSession();

return user;
}

3. your Junit method should be:


public void testSaveEditedUser(){

Users user = handleUsersController.load(new Integer(483));

user.setLastName( "saveOrUpdate" );
... the rest of the code ...

}

Remarks:

Your class Users is in fact a User (just a OOP minor detail).

Hope it will solve your problem. If not, ask for more help.


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

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.