Trabajando con Hibernate una tabla padre y una hija, en la tabla padre ingresaba correctamente pero el error surgió cuando quise ingresar en la tabla hija me dio el siguiente error:
Hibernate: 
    insert 
    into
        pedidos
        (fkclientescodcliente, fechaEntrega, fechaPedido, notas, valor) 
    values
        (?, ?, ?, ?, ?)
dic 10, 2020 3:25:34 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1452, SQLState: 23000
dic 10, 2020 3:25:34 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Cannot add or update a child row: a foreign key constraint fails (`studio`.`pedidos`, CONSTRAINT `fk_clientecodcliente` FOREIGN KEY (`fkclientescodcliente`) REFERENCES `clientes` (`codcliente`) ON DELETE NO ACTION ON UPDATE CASCADE)
Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
pase mucho tiempo tratando de encontrar la causa del error, es causado porque la tabla padre y la tabla hija tienen motor (ENGINE) diferente.
si vemos el motor de la tabla padre que en este caso es clientes en la consola de mariadb o mysql podríamos el siguiente comando (comando en negrilla y por supuesto cargando la base de datos con use <tabla> antes):
MariaDB [studio]> show create table clientes;
Nos dará el siguiente resultado:
| clientes | CREATE TABLE `clientes` (
  `codcliente` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(20) DEFAULT NULL,
  `apellido` varchar(20) DEFAULT NULL,
  `telefono` varchar(15) DEFAULT NULL,
  `direccion` varchar(50) DEFAULT NULL,
  `nit` varchar(15) DEFAULT NULL,
  `cuenta` decimal(12,2) DEFAULT NULL,
  PRIMARY KEY (`codcliente`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
si vemos la última linea  ENGINE=MyISAM tiene el motor MyISAM, ahora hacemos lo mismo con la tabla hija comando en negrilla:
MariaDB [studio]> show create table pedidos; 
| pedidos | CREATE TABLE `pedidos` (
  `codPedido` int(11) NOT NULL AUTO_INCREMENT,
  `fechaPedido` date DEFAULT NULL,
  `fechaEntrega` date DEFAULT NULL,
  `valor` decimal(12,2) DEFAULT NULL,
  `fkclientescodcliente` int(11) DEFAULT NULL,
  `notas` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`codPedido`),
  KEY `fk_clientecodcliente_idx` (`fkclientescodcliente`),
  CONSTRAINT `fk_clientecodcliente` FOREIGN KEY (`fkclientescodcliente`) REFERENCES `clientes` (`codcliente`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1 |
vemos nuevamente la última linea ENGINE=InnoDB tiene el motor InnoDB son diferentes así que deben ser iguales como InnoDB es mas moderno lo cambie en la tabla padre de la siguiente forma:
MariaDB [studio]> alter table clientes engine= innoDB;
Ahora al darle que nos muestre la creación de la tabla para ver el motor de la tabla padre (clientes) vemos que se a cambiado:
| clientes | CREATE TABLE `clientes` (
  `codcliente` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(20) DEFAULT NULL,
  `apellido` varchar(20) DEFAULT NULL,
  `telefono` varchar(15) DEFAULT NULL,
  `direccion` varchar(50) DEFAULT NULL,
  `nit` varchar(15) DEFAULT NULL,
  `cuenta` decimal(12,2) DEFAULT NULL,
  PRIMARY KEY (`codcliente`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
y ahora se habra arreglado el error 1452, me costo mucho encontrar el error solo Gracias a Dios que lo pude arreglar asi que si a alguien le ayuda esto enhorabuena.
  
 
No hay comentarios:
Publicar un comentario