2013-04-09 74 views
1

[春季MVC +休眠]插入記錄冬眠

@Controller

@Controller 
public class COAMaintenanceController { 

protected static Logger log = Logger 
     .getLogger(COAMaintenanceController.class); 

@Resource(name = "COAMaintenanceService") 
private COAMaintenanceService coaMaintenanceService; 

@RequestMapping(value = "/addCoaMaintenance", method = RequestMethod.POST) 
public @ResponseBody 
JsonResponse addCoaCategory(@RequestParam("mainAccount") long mainAccount, 
     @RequestParam("subAccount") long subAccount, 
     @RequestParam("accountName") String accountName, 
     @RequestParam("coaCategoryId") long coaCategoryId, 
     @RequestParam("postingType") int postingType, 
     @RequestParam("typicalBalance") int typicalBalance, 
     @RequestParam("isActive") int isActive) { 

    Date sysdate = null; 
    JsonResponse response = null; 

    try { 

     sysdate = new Date(); 
     response = new JsonResponse(); 

     COAMaintenanceModel coaMaintenanceModel = new COAMaintenanceModel(
       mainAccount, subAccount, accountName, coaCategoryId, 
       postingType, typicalBalance, isActive, 
       GetSessionValue.getSysUserId(), 
       GetSessionValue.getSysUserIp(), sysdate, 0); 

     coaMaintenanceService.AddCOAMaintenance(coaMaintenanceModel); 

     response.setStatus("Success"); 


    } catch (Exception ex) { 
     log.error("Exception.." + ex); 
     response.setStatus("Fail"); 

    } 

    return response; 

} 

} 

@Service

@Service("COAMaintenanceService") 
@Transactional 
public class COAMaintenanceService { 


@Resource(name="sessionFactory") 
private SessionFactory sessionFactory; 



public void AddCOAMaintenance(COAMaintenanceModel obj) { 

Session session = sessionFactory.getCurrentSession(); 
session.save(obj); 


} 

} 

控制器我寫lo可以多次輸入記錄,但以下不起作用,它只插入一條記錄。

for(int i=0; i<50; i++){ 
    coaMaintenanceService.AddCOAMaintenance(coaMaintenanceModel); 
} 

如何在上面的場景中輸入多條記錄!

回答

1

現在只有一個coaMaintenanceModel,你應該把它放在一個數組中,然後循環array.length。順便說一句,你不需要循環與一個成員的陣列

+0

好的。但我的情況是不同的。你能寄給我你的電子郵件ID嗎?我會給你舉個例子,我停下來!我的電子郵件= [email protected] – 2013-04-10 10:22:24

+0

當然,[email protected] – 2013-04-11 03:11:02