Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Hibernate:constraintName é nulo no MySQL


Cheguei à seguinte solução:

  1. Estenda o Hibernate MySQL5Dialect existente:
    public class MySQL5Dialect extends org.hibernate.dialect.MySQL5Dialect {
    
        /**
        * Pattern to extract violated constraint name from {@link SQLException}.
        */
        private static final Pattern PATTERN = Pattern.compile(".*constraint\\W+(\\w+).*", Pattern.CASE_INSENSITIVE);
    
        private ViolatedConstraintNameExtracter constraintNameExtracter;
    
        public MySQL5Dialect() {
            constraintNameExtracter = new ConstraintNameExtractor();
        }
    
        @Override
        public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
            return constraintNameExtracter;
        }
    
        private class ConstraintNameExtractor implements ViolatedConstraintNameExtracter {
    
            @Override
            public String extractConstraintName(SQLException sqle) {
                final String msg = sqle.getMessage();
                final Matcher matcher = PATTERN.matcher(msg);
                String constraintName = null;
                if (matcher.matches()) {
                    constraintName = matcher.group(1);
                }
    
                return constraintName;
            }
    
        }
    
    }
    

  2. Especifique o dialeto recém-criado no arquivo de configuração do Hibernate (hibernate.cfg.xml):
    <property name="dialect">your.package.MySQL5Dialect</property>
    

  3. Agora getConstraintName() retornará o nome real da restrição violada:
    try {
        ...
    } catch (ConstraintViolationException e) {
        LOG.error(String.format("Constraint=%s, code=%d", e.getConstraintName(), e.getErrorCode()));
    }