I need to delete an Archivo record and I have the next case. I am having problems to delete using detele() and deleteById() the code is doing nothing.
the next one is the main code:
@Override
public RespuestaVo delete(Integer archId) {
RespuestaVo respuestaVo = new RespuestaVo();
try {
if (!validarArchivoDeUsuario(archId)){
respuestaVo = new RespuestaVo(401, HttpStatus.UNAUTHORIZED, "Acceso a recursos no autorizado.");
return respuestaVo;
}
respuestaVo = new RespuestaVo(201, HttpStatus.OK, "El archivo ha sido eliminado con éxito.");
iArchivosDao.deleteById(archId);
} catch (Exception ex) {
respuestaVo = new RespuestaVo(400, HttpStatus.BAD_REQUEST, "No ha sido posible eliminar el archivo, inténtelo más tarde.");
Logger.getLogger(LogosEmprendimientosServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return respuestaVo;
}
This code use a method call "validarArchivoDeUsuario()" it is use just to validate if the archivo ouns to the user. this method do what it have to do.
private boolean validarArchivoDeUsuario(int archId) {
Personas persona = new Personas();
persona = jwtProvider.getUserID();
List<Emprendimientos> emprendimientos;
emprendimientos = iEmprendimientosDao.findByPersIdAndEmprEstado(persona, ACTIVO);
for (Emprendimientos emprendimiento: emprendimientos){
for (Archivos archivo: emprendimiento.getArchivosCollection()){
if (archivo.getArchId().equals(Integer.valueOf(archId))){
return true;
}
}
}
return false;
}
If I use the code like is now it go to the validation then go to the iArchivosDao.deleteById(archId); line but it dont do anything.(nothing in log)
But if I comment the "for" inside validarArchivoDeUsuario and hardcode the return true like next code. it also goes to iArchivosDao.deleteById(archId); line but in this case it deletes the record:
log: org.hibernate.SQL : delete from archivos where arch_id=? Hibernate: delete from archivos where arch_id=?
private boolean validarArchivoDeUsuario(int archId) {
Personas persona = new Personas();
persona = jwtProvider.getUserID();
List<Emprendimientos> emprendimientos;
emprendimientos = iEmprendimientosDao.findByPersIdAndEmprEstado(persona, ACTIVO);
/*for (Emprendimientos emprendimiento: emprendimientos){
for (Archivos archivo: emprendimiento.getArchivosCollection()){
if (archivo.getArchId().equals(Integer.valueOf(archId))){
return true;
}
}
}*/
return true;
}
Looks like some in here affect the delete. I try setting emprendimientos , archivo, emprendimiento to null before return true but didn't work
Emprendimientos.java ...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "emprId")
private Collection<Archivos> archivosCollection;
...
Archivos.java ...
@Entity
@Table(name = "ARCHIVOS")
@Data
public class Archivos {
...
@JsonBackReference("emprId-Archivo")
@JoinColumn(name = "EMPR_ID", referencedColumnName = "EMPR_ID")
@ManyToOne(optional = false)
priva
te Emprendimientos emprId;
...
Thanks for your healt!
It looks like that issue may be related to the way your JPA entities and the associated relationships are mapped in your Spring Boot application. When you uncomment the for loop inside the validarArchivoDeUsuario method, it's possible that the associated entities and relationships are loaded, and the delete operation works as expected.
try with apply the @Transactional annotation to your delete method