-->
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: Save and update object in same transaction
PostPosted: Sun Dec 04, 2011 7:32 pm 
Newbie

Joined: Sun Oct 30, 2011 5:43 am
Posts: 13
my requirement is that i want to save an object and after saving it update that object with other related data (all in same transaction, since i am using @Transcational annotation on service class level), here's a code snippet:

- Service:


Code:
@Service
@Transactional
public class MyService {`
   
       public Employee addEmployee(Employee employee) throws Exception,
                   {
   
            try {
   
                employeeDao.addEmployee(employee);
   
                MessagingGroup messagingGroup = messageGroupDao
                        .getMsgGroupByID(1);
                EmployeeGroup employeeGroup = new EmployeeGroup();
                employeeGroup.setEmployee(employee); // the saved employee
                employeeGroup.setMessageGroup(messagingGroup);
                employeeGroup.setJoinType(false);
                Set<EmployeeGroup> employeeGroups = new HashSet<EmployeeGroup>();
                employeeGroups.add(employeeGroup);
   
                employee.setEmployeeGroups(employeeGroups);
                updateEmployee(employee);
   
                return employee;
              } catch (AccessDeniedException ad) {
                throw new AccessDeniedException("Access Denied!");
              }
   
              catch (Exception e) {
                log.error("Error Occurred While Adding Employee: " + e.getMessage());
                e.printStackTrace();
                return null;
              }
   
        }
       
    }


- DAO:


Code:
public class MyDao {

    public void addEmployee(Employee employee) {
            employee = (Employee) getCurrentSession().merge(employee);
            getCurrentSession().save(employee);
        }
   
   public void updateEmployee(Employee employee) {
        getCurrentSession().update(employee);
    }

}


ISSUE: in the service, after saving the object and trying to update,i found out that the id of the saved object is 0 so it suppose that it's not saved, so it tries to insert it again, so what is the best way to avoid that, merging then saving all at once ? or make the add and the update in separate transactions ?

please advise, thanks.


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.