jeudi 27 novembre 2014

jDBCTemplate - Cannot add or update a child row: a foreign key constraint fails


Vote count:

0




I have searched and have found the generic solution to this problem but my case is special .



HTTP Status 500 - PreparedStatementCallback; SQL [INSERT INTO Casas_de_Persona (dni, idCasa) VALUES (?,?)]; Cannot add or update a child row: a foreign key constraint fails (tpspring.Casas_de_Persona, CONSTRAINT FK544B0666B4084CAB FOREIGN KEY (idCasa) REFERENCES Domicilio (id)); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (tpspring.Casas_de_Persona, CONSTRAINT FK544B0666B4084CAB FOREIGN KEY (idCasa) REFERENCES Domicilio (id))



Persona.class



@Entity
public class Persona implements Serializable {
@Id
@Column(length=20,nullable=false)
private String dni;
@Column(length=300,nullable=false)
private String apellido;
@Column(length=300,nullable=false)
private String nombre;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,orphanRemoval=true)

@JoinTable(name="Casas_de_Persona",joinColumns= @JoinColumn(name="dni"),inverseJoinColumns=@JoinColumn(name="idCasa"))
private List<Domicilio> domicilio;
//=============================
// GET AND SET
//=============================
public String getDni() {
return dni;
}
public void setDni(String dni) {
this.dni = dni;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public List<Domicilio> getDomicilio() {
return domicilio;
}
public void setDomicilio(List<Domicilio> domicilio) {
this.domicilio = domicilio;
}

//=============================
// TO STRING
//=============================


@Override
public String toString() {
return String.format(
"Persona[DNI=%d, APELLIDO='%s', NOMBRE='%s']",
dni, apellido, nombre);
}
//=============================
// END
//=============================

}


Domicilio.class



@Entity
public class Domicilio implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(nullable=false,length=300)
private String direccion;
@Column(nullable=false,length=300)
private String localidad;
@Column(nullable=false)
private boolean inundable;
@Column(nullable=false)
@Enumerated(EnumType.STRING)
private TIPOSDOMICILIO tipo_domicilio;

//=============================
// CONSTRUCTOR
//=============================
public Domicilio(String dir,String loc,boolean inun, TIPOSDOMICILIO tipo){
this.direccion=dir;
this.localidad=loc;
this.inundable=inun;
this.tipo_domicilio=tipo;
}


public Domicilio() {
// TODO Auto-generated constructor stub
}
//=============================
// GET AND SET
//=============================
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDireccion() {
return direccion;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
public String getLocalidad() {
return localidad;
}
public void setLocalidad(String localidad) {
this.localidad = localidad;
}
public boolean isInundable() {
return inundable;
}

public void setInundable(boolean inundable) {
this.inundable = inundable;
}

public TIPOSDOMICILIO getTipo_domicilio() {
return tipo_domicilio;
}

public void setTipo_domicilio(TIPOSDOMICILIO tipo_domicilio) {
this.tipo_domicilio = tipo_domicilio;
}
//=============================
// END
//=============================


The error appears when I try to perform an insert



public class DataAccessJdbcTemplateRowMapper extends JdbcDaoSupport implements DataAccess {


public void insertarPerson(Persona persona) {

String SQL = "INSERT INTO Persona (dni, apellido, nombre) VALUES (?, ?, ?)";
getJdbcTemplate().update(SQL, new Object[]{persona.getDni(), persona.getApellido(), persona.getNombre()} ); // GOOD



for(Domicilio dom : persona.getDomicilio()){
SQL = "INSERT INTO Domicilio (id, direccion, inundable, localidad, tipo_domicilio) VALUES (?, ?, ?, ?, ?)";
getJdbcTemplate().update(SQL, new Object[]{dom.getId(),dom.getDireccion(),dom.isInundable(),dom.getLocalidad(),TIPOSDOMICILIO.valueOf(dom.getTipo_domicilio().toString())} ); // GOOD

}

String SQLCasaPersona = "INSERT INTO Casas_de_Persona (dni, idCasa) VALUES (?,?)";

for(Domicilio dom : persona.getDomicilio()){
getJdbcTemplate().update(SQLCasaPersona, new Object[]{persona.getDni().toString(), dom.getId()} ); // ERROR

}



}
}


BUT if i tried to put the id manually like this



getJdbcTemplate().update(SQLCasaPersona, new Object[]{persona.getDni().toString(), "1"} );
getJdbcTemplate().update(SQLCasaPersona, new Object[]{persona.getDni().toString(), "2"} );
getJdbcTemplate().update(SQLCasaPersona, new Object[]{persona.getDni().toString(), "3"} );


works fine. I dunno why but dom.getId() is not working properly in the third query



asked 14 secs ago







jDBCTemplate - Cannot add or update a child row: a foreign key constraint fails

Aucun commentaire:

Enregistrer un commentaire