简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM