简体   繁体   中英

java.sql.SQLIntegrityConstraintViolationException Spring boot

How can i handle this exception

This is for catch error from JPA persistence

try {
    if (platform.getPlatformId() == null) {
        messageString = "inserted";
    } else {
        messageString = "updated";
    }

    Platform platf = platformServiceImpl.addOne(platform);
    model.addAttribute("locations", locationServiceImpl.getAll());

    //Date localDate = new Date();
    //PlatformDetail newDetailPlatform = new PlatformDetail();
    //newDetailPlatform.setLastUpdate(localDate);
    //newDetailPlatform.setPlatform(platform);
    //platformDetailServiceImpl.addOne(newDetailPlatform);
    //redirectAttributes.addAttribute("platformId", platf.getPlatformId());
    redirectAttributes.addFlashAttribute("success", messageString);
    return "redirect:/admin/step1/" + platf.getPlatformId();

} catch (PersistenceException ex) {
    redirectAttributes.addFlashAttribute("error", "dfdf");
    return "redirect:/admin/step1/" + ex.getCause();
}

I expected send a RedirectParameter with the key duplicate and show as a message

In the catch section, redirect just to /admin/step1 or some other endpoint with a message that something went wrong. Adding ex.getCause() makes no sense, because getCause() returns a Throwable object (to be precise: object implementing Throwable interface).

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