步骤1:在com.bstek.dorado.sample.standardlesson.service.MessageService类中增加saveAll方法,最终代码如下:
package com.bstek.dorado.sample.standardlesson.service; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.bstek.dorado.annotation.DataProvider; import com.bstek.dorado.annotation.DataResolver; import com.bstek.dorado.data.entity.EntityState; import com.bstek.dorado.data.entity.EntityUtils; import com.bstek.dorado.data.provider.Page; import com.bstek.dorado.sample.standardlesson.dao.SlEmployeeDao; import com.bstek.dorado.sample.standardlesson.dao.SlMessageDao; import com.bstek.dorado.sample.standardlesson.entity.SlEmployee; import com.bstek.dorado.sample.standardlesson.entity.SlMessage; @Component public class MessageService { @Resource private SlMessageDao slMessageDao; @Resource private SlEmployeeDao slEmployeeDao; @DataResolver @Transactional public void saveMessages(Collection<SlEmployee> slEmployees){ for(SlEmployee employee:slEmployees){ Collection<SlMessage> messages = employee.getSlMessageSet(); for(SlMessage message:messages){ //维护关联关系 message.setSlEmployee(employee); } slMessageDao.persistEntities(messages); } } @DataProvider public void getMessageByEmployeeId(Page<SlMessage> page, Integer employeeId){ String hql = "from SlMessage where slEmployee.employeeId = :employeeId"; Map param = new HashMap(); param.put("employeeId", employeeId); slMessageDao.find(page,hql,param); } @DataResolver @Transactional public void saveAll(Collection<SlEmployee> slEmployees){ //维护主表信息 slEmployeeDao.persistEntities(slEmployees); for(SlEmployee employee:slEmployees){ Collection<SlMessage> messages = employee.getSlMessageSet(); if(messages!=null){ for(SlMessage message:messages){ //维护关联关系 message.setSlEmployee(employee); } slMessageDao.persistEntities(messages); } } } }
在此方法我们可以看到通过UpdateAction将slEmployees集合对象传递到后台,然后我们遍历集合从员工对象中取得消息集合,然后持久化消息。另外要注意因为JPA映射配置时主从关系的原因,我们需要维护员工和消息的关联关系。