Dorado 7 : 12.3.2.2 准备后台服务

步骤1:在com.bstek.dorado.sample.standardlesson.service包中新建MessageService.java,代码如下:

package com.bstek.dorado.sample.standardlesson.service;

import java.util.Collection;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.bstek.dorado.annotation.DataResolver;
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;
    
    @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);
        }
    }
}

在此方法我们可以看到通过UpdateAction将slEmployees集合对象传递到后台,然后我们遍历集合从员工对象中取得消息集合,然后持久化消息。另外要注意因为JPA映射配置时主从关系的原因,我们需要维护员工和消息的关联关系。