Example usage for org.springframework.dao DataIntegrityViolationException getMessage

List of usage examples for org.springframework.dao DataIntegrityViolationException getMessage

Introduction

In this page you can find the example usage for org.springframework.dao DataIntegrityViolationException getMessage.

Prototype

@Override
@Nullable
public String getMessage() 

Source Link

Document

Return the detail message, including the message from the nested exception if there is one.

Usage

From source file:ch.wisv.areafiftylan.exception.GlobalControllerExceptionHandler.java

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<?> handleDataIntegrityViolation(DataIntegrityViolationException ex) {
    return createResponseEntity(HttpStatus.CONFLICT, ex.getMessage());
}

From source file:android.apn.androidpn.server.service.impl.UserServiceImpl.java

public User saveUser(User user) throws UserExistsException {
    try {//w w w.  j  a  v a  2 s. c o  m
        return userDao.saveUser(user);
    } catch (DataIntegrityViolationException e) {
        e.printStackTrace();
        log.warn(e.getMessage());
        throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
    } catch (EntityExistsException e) { // needed for JPA
        e.printStackTrace();
        log.warn(e.getMessage());
        throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
    }
}

From source file:com.yilv.service.impl.UserServiceImpl.java

@Transactional(readOnly = false)
public User saveUser(User user) throws UserExistsException {
    try {/* ww  w . jav a  2  s  . c o m*/
        return userDao.saveUser(user);
    } catch (DataIntegrityViolationException e) {
        e.printStackTrace();
        log.warn(e.getMessage());
        throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
    } catch (EntityExistsException e) { // needed for JPA
        e.printStackTrace();
        log.warn(e.getMessage());
        throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
    }
}

From source file:com.todo.backend.web.rest.exception.ExceptionResolver.java

@ResponseStatus(value = HttpStatus.CONFLICT)
@ExceptionHandler(DataIntegrityViolationException.class)
public @ResponseBody ErrorResponse dataIntegrityException(HttpServletRequest request,
        DataIntegrityViolationException exception) {
    if (log.isErrorEnabled()) {
        log.error(exception.getMessage(), exception);
    }/*from   w  w  w. j  ava 2  s  .  c o m*/
    // example: Duplicate entry 'field1-field2' for key 'UNQ_MODE_F1_F2_251F9D'
    final String message = exception.getRootCause().getMessage();
    if (message.contains("'UNQ")) {
        final String constraint = message.substring(message.indexOf("'UNQ") + 1, message.lastIndexOf("'"));
        return new ErrorResponse(ConstraintMapping.getErrorCodeForConstraint(constraint), message);
    }
    return new ErrorResponse(message, Collections.emptyList());
}

From source file:br.eti.danielcamargo.backend.common.rest.handlers.GlobalExceptionHandler.java

@ExceptionHandler
public ResponseEntity<List<Mensagem>> handleException(DataIntegrityViolationException ex) {

    String constraint = null;/*w  w  w  .j a v  a 2s . c  om*/
    Mensagem mensagem = null;

    String msg = ex.getMessage();
    Pattern p = Pattern.compile("\\[(\\w+_ukey)\\]");
    Matcher m = p.matcher(msg);

    if (m.find()) {
        constraint = m.group(1);
        mensagem = MessageUtils.criarMensagemViolacaoUnicidade("common", constraint);
    }

    p = Pattern.compile("\\[(\\w+_pkey)\\]");
    m = p.matcher(msg);

    if (m.find()) {
        constraint = m.group(1);
        mensagem = MessageUtils.criarMensagemViolacaoChavePrimaria("common", constraint);
    }

    p = Pattern.compile("\\[(\\w+_fkey)\\]");
    m = p.matcher(msg);

    if (m.find()) {
        constraint = m.group(1);
        mensagem = MessageUtils.criarMensagemViolacaoChaveEstrangeira("common", constraint);
    }

    List<Mensagem> list = new ArrayList<>();
    list.add(mensagem);

    ResponseEntity<List<Mensagem>> responseEntity = new ResponseEntity<>(list, HttpStatus.BAD_REQUEST);
    return responseEntity;

}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.PersonDAOTest.java

@Test
public final void testCreatePersonWithValidationFailed() {
    final IPersonDAO personDAO = daoFactory.getPersonDAO();
    final PersonPE testPerson = createPerson();
    testPerson.setUserId(StringUtils.repeat("A", 51));
    // User id too long
    try {/*from  w  ww  . j  a v  a 2  s .c  om*/
        personDAO.createPerson(testPerson);
        fail("User id exceeds the maximum length");
    } catch (final DataIntegrityViolationException ex) {
        assertTrue(ex.getMessage().indexOf("is too long") > -1);
    }
}

From source file:com.br.helpdesk.controller.UserGroupController.java

@ExceptionHandler(DataIntegrityViolationException.class)
public void handleDataIntegrityViolationException(DataIntegrityViolationException ex,
        HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage());
}

From source file:com.mazmy.service.manufacturer.DefaultManufacturerService.java

/**
  * Creates a new Manufacturer.//  w w w  . ja v  a  2 s .  c  o m
  *
  * @param ManufacturerDO
  * @return
  * @throws ConstraintsViolationException if a Manufacturer already exists with the given licensePlate, ... .
  */
@Override
public ManufacturerDO create(ManufacturerDO manufacturerDO) throws ConstraintsViolationException {
    ManufacturerDO manufacturer = null;
    try {
        manufacturer = manufacturerRepository.save(manufacturerDO);
    } catch (DataIntegrityViolationException e) {
        LOG.warn("Some constraints are thrown due to manufacturer creation", e);
        throw new ConstraintsViolationException(e.getMessage());
    }
    return manufacturer;
}

From source file:org.openmrs.contrib.metadatarepository.service.impl.UserManagerImpl.java

/**
 * {@inheritDoc}//from   ww  w  . j  a v a2  s .co  m
 */
public User saveUser(User user) throws UserExistsException {

    if (user.getVersion() == null) {
        // if new user, lowercase userId
        user.setUsername(user.getUsername().toLowerCase());
    }

    // Get and prepare password management-related artifacts
    boolean passwordChanged = false;
    if (passwordEncoder != null) {
        // Check whether we have to encrypt (or re-encrypt) the password
        if (user.getVersion() == null) {
            // New user, always encrypt
            passwordChanged = true;
        } else {
            // Existing user, check password in DB
            String currentPassword = userDao.getUserPassword(user.getUsername());
            if (currentPassword == null) {
                passwordChanged = true;
            } else {
                if (!currentPassword.equals(user.getPassword())) {
                    passwordChanged = true;
                }
            }
        }

        // If password was changed (or new user), encrypt it
        if (passwordChanged) {
            user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
        }
    } else {
        log.warn("PasswordEncoder not set, skipping password encryption...");
    }

    try {
        return userDao.saveUser(user);
    } catch (DataIntegrityViolationException e) {
        //e.printStackTrace();
        log.warn(e.getMessage());
        throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
    } catch (JpaSystemException e) { // needed for JPA
        //e.printStackTrace();
        log.warn(e.getMessage());
        throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
    }
}

From source file:se.sawano.spring.examples.springdata.UserRepositoryTestIT.java

@Test(expected = DataIntegrityViolationException.class)
public void repositoryShouldCheckNullConstraintForFirstName() throws Exception {
    User user = new User(null, "LastName");
    try {//ww  w. ja  v a 2s  .com
        userRepository.save(user);
        fail();
    } catch (DataIntegrityViolationException e) {
        assertTrue("Could not find column name in error message",
                e.getMessage().contains("USER column: FIRSTNAME"));
        throw e;
    }
}