Example usage for org.hibernate.exception GenericJDBCException getSQL

List of usage examples for org.hibernate.exception GenericJDBCException getSQL

Introduction

In this page you can find the example usage for org.hibernate.exception GenericJDBCException getSQL.

Prototype

public String getSQL() 

Source Link

Document

Get the actual SQL statement being executed when the exception occurred.

Usage

From source file:motor.MOTOR.java

/**
 * Mtodo que inserta una registro en la tabla cliente. Utiliza operaciones
 * elementales//from  www .j a v a 2 s . c om
 *
 * @param cliente el coche a insertar
 * @throws ExceptionMotor excepcion que integra el mensaje de error al
 * usuario, el codigo de error y el mensaje de error que nos ha devuelto la
 * base de datos.
 */
public void insertarCliente(Cliente cliente) throws ExceptionMotor {
    // Se inicia una transaccin en la sesin creada
    Transaction t = sesion.beginTransaction();

    try {

        sesion.save(cliente);
        // Se confirma la transaccin
        t.commit();
    } catch (IdentifierGenerationException igEx) {
        ExceptionMotor ex = new ExceptionMotor();
        throw ex;
    } catch (ConstraintViolationException igEx) {
        ExceptionMotor ex = new ExceptionMotor();
        if (igEx.getErrorCode() == 2290) {
            ex.setCodigoErrorAdministrador(igEx.getErrorCode());
            ex.setSentenciaSQL(igEx.getSQL());
            ex.setMensajeErrorUsuario(
                    "Error. El campo telefono debe empezar por 6 o 9 y contener 9 caracteres numricos y el email debe acabar en .com o .es");
            ex.setMensajeErrorAdministrador(igEx.getMessage());
        } else {
            ex.setCodigoErrorAdministrador(igEx.getErrorCode());
            ex.setSentenciaSQL(igEx.getSQL());
            ex.setMensajeErrorUsuario("Error. Los siguientes campos son nicos: dni, email y telefono");
            ex.setMensajeErrorAdministrador(igEx.getMessage());
        }
        throw ex;
    } catch (PropertyValueException pvEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setMensajeErrorUsuario(
                "Error. Los siguientes campos son obligatorios: nombre, apellido1, email, telefono, dni");
        ex.setMensajeErrorAdministrador(pvEx.getMessage());
        throw ex;
    } catch (SQLGrammarException sqlEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setCodigoErrorAdministrador(sqlEx.getErrorCode());
        ex.setSentenciaSQL(sqlEx.getSQL());
        ex.setMensajeErrorUsuario("Error. El campo telefono es un campo numerico");
        ex.setMensajeErrorAdministrador(sqlEx.getMessage());

        throw ex;
    } catch (GenericJDBCException jdbcEx) {
        ExceptionMotor ex = new ExceptionMotor();
        if (jdbcEx.getErrorCode() == 20004) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El taller esta cerrado");
            ex.setMensajeErrorAdministrador(jdbcEx.getMessage());
        }
        if (jdbcEx.getErrorCode() == 20005) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El email debe acabar por .com o .es");
            ex.setMensajeErrorAdministrador(jdbcEx.getMessage());
        }
        if (jdbcEx.getErrorCode() == 20006) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El campo telefono es un campo numerico de 9 caracteres");
            ex.setMensajeErrorAdministrador(jdbcEx.getMessage());
        }

        throw ex;
    } catch (Exception e) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setCodigoErrorAdministrador(0);
        ex.setMensajeErrorUsuario("Error general en el sistema, Consulte con el administrador");
        ex.setMensajeErrorAdministrador(e.getMessage());

        throw ex;
    }

}

From source file:motor.MOTOR.java

/**
 * Mtodo que modifica los registros de un cliente ya existente. Utiliza
 * operaciones elementales/*  w  ww.  j  a  va 2  s .c om*/
 *
 * @param cliente los datos del coche a modificar
 * @throws ExceptionMotor excepcion que integra el mensaje de error al
 * usuario, el codigo de error y el mensaje de error que nos ha devuelto la
 * base de datos
 */
public void modificarCliente(Cliente cliente) throws ExceptionMotor {
    Transaction t = sesion.beginTransaction();

    try {

        sesion.update(cliente);
        // Se confirma la transaccin
        t.commit();

    } catch (ConstraintViolationException cvEx) {
        ExceptionMotor ex = new ExceptionMotor();

        if (cvEx.getErrorCode() == 2290) {
            ex.setCodigoErrorAdministrador(cvEx.getErrorCode());
            ex.setSentenciaSQL(cvEx.getSQL());
            ex.setMensajeErrorUsuario(
                    "Error. El campo telefono debe empezar por 6 o 9 y contener 9 caracteres numricos y el email debe acabar en .com o .es");
            ex.setMensajeErrorAdministrador(cvEx.getMessage());

        } else {
            ex.setCodigoErrorAdministrador(cvEx.getErrorCode());
            ex.setSentenciaSQL(cvEx.getSQL());
            ex.setMensajeErrorUsuario("Error. Los siguientes campos son unicos: dni, email y telefono");
            ex.setMensajeErrorAdministrador(cvEx.getMessage());
        }
        throw ex;
    } catch (PropertyValueException pvEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setMensajeErrorUsuario(
                "Error. Los siguientes campos son obligatorios: nombre, apellido1, email, telefono, dni");
        ex.setMensajeErrorAdministrador(pvEx.getMessage());

        throw ex;
    } catch (SQLGrammarException sqlEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setCodigoErrorAdministrador(sqlEx.getErrorCode());
        ex.setSentenciaSQL(sqlEx.getSQL());
        ex.setMensajeErrorUsuario("Error. El telefono es un campo numerico");
        ex.setMensajeErrorAdministrador(sqlEx.getMessage());

        throw ex;
    } catch (GenericJDBCException jdbcEx) {
        ExceptionMotor ex = new ExceptionMotor();

        if (jdbcEx.getErrorCode() == 20004) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El taller esta cerrado");
            ex.setMensajeErrorAdministrador(jdbcEx.getMessage());
        }
        if (jdbcEx.getErrorCode() == 20005) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El email debe acabar por .com o .es");
            ex.setMensajeErrorAdministrador(jdbcEx.getMessage());
        }
        if (jdbcEx.getErrorCode() == 20006) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El campo telefono es un campo numerico de 9 caracteres");
            ex.setMensajeErrorAdministrador(jdbcEx.getMessage());
        }
        throw ex;
    } catch (StaleStateException ssEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setMensajeErrorUsuario("Error. No existe ese cliente");

        throw ex;
    } catch (Exception e) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setCodigoErrorAdministrador(0);
        ex.setMensajeErrorUsuario("Error general en el sistema, Consulte con el administrador");
        ex.setMensajeErrorAdministrador(e.getMessage());

        throw ex;
    }

}

From source file:motor.MOTOR.java

/**
 * Mtodo que inserta un registro en la tabla coche. Utiliza operaciones
 * elementales/*from  w  w w .ja va 2  s.c o m*/
 *
 * @param coche El objeto coche
 * @throws ExceptionMotor excepcion que integra el mensaje de error al
 * usuario, el codigo de error y el mensaje de error que nos ha devuelto la
 * base de datos
 */
public void insertarCoche(Coche coche) throws ExceptionMotor {
    // Se inicia una transaccin en la sesin creada
    Transaction t = sesion.beginTransaction();

    try {

        sesion.save(coche);
        // Se confirma la transaccin
        t.commit();
    } catch (PropertyValueException pvEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setMensajeErrorUsuario("Error. Todos los campos son obligatorios");
        ex.setMensajeErrorAdministrador(pvEx.getMessage());

        throw ex;

    } catch (ConstraintViolationException igEx) {
        ExceptionMotor ex = new ExceptionMotor();

        if (igEx.getErrorCode() == 2291) {
            ex.setCodigoErrorAdministrador(igEx.getErrorCode());
            ex.setSentenciaSQL(igEx.getSQL());
            ex.setMensajeErrorUsuario("Error. No existe ese cliente");
            ex.setMensajeErrorAdministrador("VIOLACION DE CHECK CONSTRAINT");
        } else if (igEx.getErrorCode() == 1) {
            ex.setCodigoErrorAdministrador(igEx.getErrorCode());
            ex.setSentenciaSQL(igEx.getSQL());
            ex.setMensajeErrorUsuario("Error. Los siguientes campos son unicos: matricula");
            ex.setMensajeErrorAdministrador("VIOLACION DE UNIQUE KEY");
        }
        throw ex;

    } catch (GenericJDBCException gjdbcEx) {
        ExceptionMotor ex = new ExceptionMotor();

        if (gjdbcEx.getErrorCode() == 20002) {
            ex.setCodigoErrorAdministrador(gjdbcEx.getErrorCode());
            ex.setSentenciaSQL(gjdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. La ITV debe estar pasada");
            ex.setMensajeErrorAdministrador("VIOLACION DE TRIGGER ITV_PASADA");
        }
        if (gjdbcEx.getErrorCode() == 20004) {
            ex.setCodigoErrorAdministrador(gjdbcEx.getErrorCode());
            ex.setSentenciaSQL(gjdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El taller esta cerrado");
            ex.setMensajeErrorAdministrador("VIOLACION DE TRIGGER TALLER_ABIERTO");
        }
        throw ex;

    } catch (Exception e) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setCodigoErrorAdministrador(0);
        ex.setMensajeErrorUsuario("Error general en el sistema, Consulte con el administrador");
        ex.setMensajeErrorAdministrador(e.getMessage());

        throw ex;
    }

}

From source file:motor.MOTOR.java

/**
 * Mtodo que modifica los datos de un coche ya existente. Usa HQL
 * parametrizado//from  www  .ja  v a2 s.  c  om
 *
 * @return el numero de registros afectados
 * @param coche el objeto coche con los datos a modificar
 * @throws ExceptionMotor excepcion que integra el mensaje de error al
 * usuario, el codigo de error y el mensaje de error que nos ha devuelto la
 * base de datos
 */
public int modificarCoche(Coche coche) throws ExceptionMotor {
    int registrosAfectados = -1;

    // Se inicia una transaccin en la sesin creada
    Transaction t = sesion.beginTransaction();

    // Se crea la query HQL
    Query q = sesion.createQuery("update Coche set cliente.clienteId = :coclienteId,"
            + "marca = :comarca, modelo = :comodelo, matricula = :comatricula , " + "itv = :coitv "
            + "where cocheId = :cococheId");
    q.setString("comarca", coche.getMarca());
    q.setString("comodelo", coche.getModelo());
    q.setString("comatricula", coche.getMatricula());
    q.setDate("coitv", coche.getItv());
    q.setBigDecimal("cococheId", coche.getCocheId());
    q.setBigDecimal("coclienteId", coche.getCliente().getClienteId());

    try {
        // Se ejecuta la query q
        registrosAfectados = q.executeUpdate();

        // Se confirma la transaccin
        t.commit();
    } catch (GenericJDBCException jdbcEx) {
        ExceptionMotor ex = new ExceptionMotor();

        if (jdbcEx.getErrorCode() == 2291) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. No existe ese ciente");
            ex.setMensajeErrorAdministrador("NO SE HA INTRODUCIDO UN COCHE_ID QUE EXISTA");
        }
        if (jdbcEx.getErrorCode() == 1407) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario(
                    "Error. Los siguientes campos son obligastorios: marca, modelo, matricula, itv");
            ex.setMensajeErrorAdministrador("VIOLACION DE NOT_NULL");
        }
        if (jdbcEx.getErrorCode() == 20002) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. La ITV debe estar pasada");
            ex.setMensajeErrorAdministrador("VILACION DE TRIGGER ITV_PASADA");
        }
        if (jdbcEx.getErrorCode() == 20004) {
            ex.setCodigoErrorAdministrador(jdbcEx.getErrorCode());
            ex.setSentenciaSQL(jdbcEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El taller esta cerrado");
            ex.setMensajeErrorAdministrador("VIOLACION DE TRIGGER TALLER ABIERTO");
        }
        throw ex;
    } catch (ConstraintViolationException cvEx) {
        ExceptionMotor ex = new ExceptionMotor();

        if (cvEx.getErrorCode() == 1) {
            ex.setCodigoErrorAdministrador(cvEx.getErrorCode());
            ex.setSentenciaSQL(cvEx.getSQL());
            ex.setMensajeErrorUsuario("Error. El campo matricula es unico");
            ex.setMensajeErrorAdministrador("VIOLACION DE UNIQUE KEY");
        }
        throw ex;
    } catch (StaleStateException ssEx) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setMensajeErrorUsuario("Error. No existe ese coche");
        ex.setMensajeErrorAdministrador("SE HA INTRODUCIDO UN COCHE_ID QUE NO EXISTE");
        throw ex;
    } catch (Exception e) {
        ExceptionMotor ex = new ExceptionMotor();

        ex.setCodigoErrorAdministrador(0);
        ex.setMensajeErrorUsuario("Error general en el sistema, Consulte con el administrador");
        ex.setMensajeErrorAdministrador(e.getMessage());

        throw ex;
    }
    return registrosAfectados;
}