ERROR: Can not add or update to child row: a foreign key constraint fails

0

I would like to ask if someone has experience in hibernate. Why theoretically this until two days ago was working correctly. (theoretically why I was developing and did a single test, which saved the data correctly and then did not probe until now). Now when I want to test to be able to put this piece of code in production I skip the error cited below.

I leave the mappings where the error occurs and if necessary I will load all the complete entities to see what happens.

In case something comes to help I'm working with jboss tools and hibernate 5.2.3 (if I'm not mistaken that is the version of the library that I own), any comment to help is appreciated.

//tabla Turnos
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "paciente", nullable = false)
public Paciente getPaciente() {
    return this.paciente;
}

public void setPaciente(Paciente paciente) {
    this.paciente = paciente;
}


//tabla Paciente
@OneToMany(fetch = FetchType.LAZY, mappedBy = "paciente")
public Set<Turnos> getTurnoses() {
    return this.turnoses;
}

public void setTurnoses(Set<Turnos> turnoses) {
    this.turnoses = turnoses;
}

XML Turns

    <many-to-one name="paciente" class="com.historiasclinicas.entidades.Paciente" fetch="select">
        <column name="paciente" not-null="true" />
    </many-to-one>

XML Patient

   <set name="turnoses" table="turnos" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="paciente" not-null="true" />
        </key>
        <one-to-many class="com.historiasclinicas.entidades.Turnos" />
    </set>

// event save shift

public static int NuevoTurno(Integer DNI, String fecha, String especialista, String horario) throws IOException {
    SessionFactory factory;
    int hecho = 0;
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (HibernateException he) {
        Log.CreaLog(he.getMessage());
        System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
        throw new ExceptionInInitializerError(he);
    }
    Session session = factory.openSession();

    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        Turnos turno = new Turnos();
        List<Paciente> paciente = GestorPacientes.ConsultaInexistencia(DNI);
        //al paciente ya lo traigo en una lista, por que comparto con otras pantallas el mismo evento
        Estados estado = GestorEstados.ConsultarEstado(1);
        turno.setPaciente(paciente.get(0));
        turno.setFechaTurno(fecha);
        turno.setEspecialista(especialista);
        turno.setEstados(estado);
        turno.setHoraTurno(horario);

        try {
            session.save(turno);
            hecho = 1;
            Log.CreaLog("Turno Ingresado con Exito");
            return hecho;
        } catch (Exception e) {
            Log.CreaLog(e.getMessage().toString());
            hecho = 0;
            return hecho;
        }
    } catch (HibernateException e) {
        if (transaction != null)
            hecho = 0;
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return hecho;
}
  

ERROR: Can not add or update a child row: a foreign key constraint fails ( histocons . turnos , CONSTRAINT pacientes FOREIGN KEY ( paciente ) REFERENCES paciente ( dni ) ON DELETE NO ACTION ON UPDATE NO ACTION)

    
asked by Pablo Ezequiel Ferreyra 01.12.2016 в 09:56
source

1 answer

0

And where is the @Id ?

If you have not assigned the table shifts , add it; if you have it, I guess you should have it to be a AUTO_INCREMENT which is:

@Id
@GeneratedValue(strategy = GenerationStrategy.IDENTITY)
private long id;
    
answered by 13.12.2016 в 05:05