Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 在同一事务中保存和更新对象_Hibernate_Jakarta Ee - Fatal编程技术网

Hibernate 在同一事务中保存和更新对象

Hibernate 在同一事务中保存和更新对象,hibernate,jakarta-ee,Hibernate,Jakarta Ee,我的要求是,我希望保存一个对象,并在保存后在同一事务中使用其他相关数据更新该对象,因为我在服务类级别上使用@Transconational注释,下面是一个代码片段: 服务: 问题: 在服务中,在保存对象并尝试更新后,我发现保存的对象的ID为0,因此它假设未保存该对象。我猜是这样,因为在更新之前事务尚未提交,因为保存和更新都在同一事务中,所以它尝试再次插入该对象,那么最好的方法是什么,合并然后一次保存所有内容?或者在单独的事务中进行添加和更新,或者使用saveOrUpdate方法?我很困惑 请告知

我的要求是,我希望保存一个对象,并在保存后在同一事务中使用其他相关数据更新该对象,因为我在服务类级别上使用@Transconational注释,下面是一个代码片段:

服务: 问题: 在服务中,在保存对象并尝试更新后,我发现保存的对象的ID为0,因此它假设未保存该对象。我猜是这样,因为在更新之前事务尚未提交,因为保存和更新都在同一事务中,所以它尝试再次插入该对象,那么最好的方法是什么,合并然后一次保存所有内容?或者在单独的事务中进行添加和更新,或者使用saveOrUpdate方法?我很困惑


请告知,谢谢

我认为您也应该尝试在Dao上使用@Transactional annotation。

通过更改save Dao方法以返回带有生成标识符的已保存对象来修复此问题:

public Employee addEmployee(Employee employee) {
            employee = (Employee) getCurrentSession().merge(employee);
            getCurrentSession().save(employee);
            return employee;
        }
在服务中:

            employee=employeeDao.addEmployee(employee); // to get the generated identifier

            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);

是否保存pk之外的其他字段?奇怪的是,在这种情况下它是0。pk是什么样的栏目?您是否将其定义为自动增量?您的意思是在dao类级别上?在dao中的add和update方法上尝试过,但没有运气,同样的问题。我假设您使用的是spring框架,如果是这样,您必须将@Transconational注释放在接口上,而不是实现类上。
public Employee addEmployee(Employee employee) {
            employee = (Employee) getCurrentSession().merge(employee);
            getCurrentSession().save(employee);
            return employee;
        }
            employee=employeeDao.addEmployee(employee); // to get the generated identifier

            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);