当前位置: 动力学知识库 > 问答 > 编程问答 >

java - insert record using for loop with spring mvc + hibernate

问题描述:

[Spring MVC + Hibernate]

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

}

}

In Controller I write loop to enter record more than one time, but the following is not working , it insert only one record.

for(int i=0; i<50; i++){

coaMaintenanceService.AddCOAMaintenance(coaMaintenanceModel);

}

How to enter the multiple records in the above scenerio!

网友答案:

There is only one coaMaintenanceModel at this time, you should put it in an array, and loop by array.length. by the way, you don't need to loop an array with one member

分享给朋友:
您可能感兴趣的文章:
随机阅读: