Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/02/2013, 13:26
ARAPSPGON
 
Fecha de Ingreso: octubre-2007
Mensajes: 57
Antigüedad: 18 años, 11 meses
Puntos: 0
Pregunta JPA Controladores de Entidades Relacionadas

Buenas tengo el siguiente problema cuando intento borrar un registro de la bd.

la base de datos se compone de dos tablas

Departamentos
Empleados

estan relacionadas 1:N con un restricción en la clave foranea en cascada para actualizaciones y borrados...si borro un departamento se borran los registros empleados que pertenecen a ese departamento..

aqui el script SQL

Código PHP:
DROP SCHEMA IF EXISTS `empresabd` ;
CREATE SCHEMA IF NOT EXISTS `empresabd` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `
empresabd` ;

-- -----------------------------------------------------
-- 
Table `empresabd`.`departamentos`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `empresabd`.`departamentos` ;

CREATE  TABLE IF NOT EXISTS `empresabd`.`departamentos` (
  `
iddep` INT NOT NULL AUTO_INCREMENT ,
  `
categoria` VARCHAR(45) NOT NULL ,
  `
descripcion` VARCHAR(200) NOT NULL ,
  
PRIMARY KEY (`iddep`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- 
Table `empresabd`.`empleados`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `empresabd`.`empleados` ;

CREATE  TABLE IF NOT EXISTS `empresabd`.`empleados` (
  `
dni` VARCHAR(10) NOT NULL ,
  `
nombre` VARCHAR(40) NOT NULL ,
  `
apellidos` VARCHAR(150) NOT NULL ,
  `
direccion` VARCHAR(200) NOT NULL ,
  `
tel` VARCHAR(10) NOT NULL ,
  `
iddep` INT NOT NULL ,
  
PRIMARY KEY (`dni`) ,
  
INDEX `fk_empleados_departamentos` (`iddep` ASC) ,
  
CONSTRAINT `fk_empleados_departamentos`
    
FOREIGN KEY (`iddep` )
    
REFERENCES `empresabd`.`departamentos` (`iddep` )
    
ON DELETE CASCADE
    ON UPDATE CASCADE
)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

CREATE USER 'userempresabd'@'localhost' IDENTIFIED BY '123';
GRANT SELECT , 
INSERT ,
UPDATE ,
DELETE ON `empresabd` . * TO 'userempresabd'@'localhost';

INSERT INTO departamentos (categoria, descripcion) VALUES 
('1a', 'RECURSOS HUMANOS'),
(
'1b', 'ID+D');

INSERT INTO empleados (dni, nombre, apellidos, direccion, tel, iddep) VALUES
('11111111D', 'Pablo', 'Arjona', 'Calle 2', '000111222', '1'),
(
'22222222D', 'Ana', 'Arjone', 'Calle 3', '000222333', '2'); 
en netbeans genero las clases entidad a partir de la base de datos:

Clase entidad Departamentos

Código PHP:
package Entidades;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@
Entity
@Table(name = "departamentos")
@
XmlRootElement
@NamedQueries({
    @
NamedQuery(name = "Departamentos.findAll", query = "SELECT d FROM Departamentos d"),
    @
NamedQuery(name = "Departamentos.findByIddep", query = "SELECT d FROM Departamentos d WHERE d.iddep = :iddep"),
    @
NamedQuery(name = "Departamentos.findByCategoria", query = "SELECT d FROM Departamentos d WHERE d.categoria = :categoria"),
    @
NamedQuery(name = "Departamentos.findByDescripcion", query = "SELECT d FROM Departamentos d WHERE d.descripcion = :descripcion")})
public class 
Departamentos implements Serializable {
    private static final 
long serialVersionUID = 1L;
    @
Id
    
@GeneratedValue(strategy = GenerationType.IDENTITY)
    @
Basic(optional = false)
    @
Column(name = "iddep")
    private 
Integer iddep;
    @
Basic(optional = false)
    @
Column(name = "categoria")
    private 
String categoria;
    @
Basic(optional = false)
    @
Column(name = "descripcion")
    private 
String descripcion;
    @
OneToMany(cascade = CascadeType.ALL, mappedBy = "iddep")
    private 
Collection<Empleados> empleadosCollection;

    public 
Departamentos() {
    }

    public 
Departamentos(Integer iddep) {
        
this.iddep = iddep;
    }

    public 
Departamentos(Integer iddep, String categoria, String descripcion) {
        
this.iddep = iddep;
        
this.categoria = categoria;
        
this.descripcion = descripcion;
    }

    public 
Integer getIddep() {
        return 
iddep;
    }

    public 
void setIddep(Integer iddep) {
        
this.iddep = iddep;
    }

    public 
String getCategoria() {
        return 
categoria;
    }

    public 
void setCategoria(String categoria) {
        
this.categoria = categoria;
    }

    public 
String getDescripcion() {
        return 
descripcion;
    }

    public 
void setDescripcion(String descripcion) {
        
this.descripcion = descripcion;
    }

    @
XmlTransient
    
public Collection<Empleados> getEmpleadosCollection() {
        return 
empleadosCollection;
    }

    public 
void setEmpleadosCollection(Collection<Empleados> empleadosCollection) {
        
this.empleadosCollection = empleadosCollection;
    }

    @
Override
    
public int hashCode() {
        
int hash = 0;
        
hash += (iddep != null ? iddep.hashCode() : 0);
        return 
hash;
    }

    @
Override
    
public boolean equals(Object object) {
        
// TODO: Warning - this method won't work in the case the id fields are not set
        
if (!(object instanceof Departamentos)) {
            return 
false;
        }
        
Departamentos other = (Departamentos) object;
        if ((
this.iddep == null && other.iddep != null) || (this.iddep != null && !this.iddep.equals(other.iddep))) {
            return 
false;
        }
        return 
true;
    }

    @
Override
    
public String toString() {
        return 
"Entidades.Departamentos[ iddep=" + iddep + " ]";
    }
    
} 
Clase entidad Empleados

Código PHP:
package Entidades;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@
Entity
@Table(name = "empleados")
@
XmlRootElement
@NamedQueries({
    @
NamedQuery(name = "Empleados.findAll", query = "SELECT e FROM Empleados e"),
    @
NamedQuery(name = "Empleados.findByDni", query = "SELECT e FROM Empleados e WHERE e.dni = :dni"),
    @
NamedQuery(name = "Empleados.findByNombre", query = "SELECT e FROM Empleados e WHERE e.nombre = :nombre"),
    @
NamedQuery(name = "Empleados.findByApellidos", query = "SELECT e FROM Empleados e WHERE e.apellidos = :apellidos"),
    @
NamedQuery(name = "Empleados.findByDireccion", query = "SELECT e FROM Empleados e WHERE e.direccion = :direccion"),
    @
NamedQuery(name = "Empleados.findByTel", query = "SELECT e FROM Empleados e WHERE e.tel = :tel")})
public class 
Empleados implements Serializable {
    private static final 
long serialVersionUID = 1L;
    @
Id
    
@Basic(optional = false)
    @
Column(name = "dni")
    private 
String dni;
    @
Basic(optional = false)
    @
Column(name = "nombre")
    private 
String nombre;
    @
Basic(optional = false)
    @
Column(name = "apellidos")
    private 
String apellidos;
    @
Basic(optional = false)
    @
Column(name = "direccion")
    private 
String direccion;
    @
Basic(optional = false)
    @
Column(name = "tel")
    private 
String tel;
    @
JoinColumn(name = "iddep", referencedColumnName = "iddep")
    @
ManyToOne(optional = false)
    private 
Departamentos iddep;

    public 
Empleados() {
    }

    public 
Empleados(String dni) {
        
this.dni = dni;
    }

    public 
Empleados(String dni, String nombre, String apellidos, String direccion, String tel) {
        
this.dni = dni;
        
this.nombre = nombre;
        
this.apellidos = apellidos;
        
this.direccion = direccion;
        
this.tel = tel;
    }

    public 
String getDni() {
        return 
dni;
    }

    public 
void setDni(String dni) {
        
this.dni = dni;
    }

    public 
String getNombre() {
        return 
nombre;
    }

    public 
void setNombre(String nombre) {
        
this.nombre = nombre;
    }

    public 
String getApellidos() {
        return 
apellidos;
    }

    public 
void setApellidos(String apellidos) {
        
this.apellidos = apellidos;
    }

    public 
String getDireccion() {
        return 
direccion;
    }

    public 
void setDireccion(String direccion) {
        
this.direccion = direccion;
    }

    public 
String getTel() {
        return 
tel;
    }

    public 
void setTel(String tel) {
        
this.tel = tel;
    }

    public 
Departamentos getIddep() {
        return 
iddep;
    }

    public 
void setIddep(Departamentos iddep) {
        
this.iddep = iddep;
    }

    @
Override
    
public int hashCode() {
        
int hash = 0;
        
hash += (dni != null ? dni.hashCode() : 0);
        return 
hash;
    }

    @
Override
    
public boolean equals(Object object) {
        
// TODO: Warning - this method won't work in the case the id fields are not set
        
if (!(object instanceof Empleados)) {
            return 
false;
        }
        
Empleados other = (Empleados) object;
        if ((
this.dni == null && other.dni != null) || (this.dni != null && !this.dni.equals(other.dni))) {
            return 
false;
        }
        return 
true;
    }

    @
Override
    
public String toString() {
        return 
"Entidades.Empleados[ dni=" + dni + " ]";
    }
    
} 
....sigue en siguiente post