-2

While trying to execute a delete query on registers of the class Alumno, on a many to one relationship with the class Calificacion I get null pointer exception on the autowired repository.

public class AlumnoListener {

    @Autowired
    CalificacionRepositorio calificacionDAO;
    
    @PreRemove
    public void borradoAlumno(Alumno alumno) {
        
        List<Calificacion> list = (List<Calificacion>) calificacionDAO.findAll();

        list.stream().filter(c -> c.getAlumno().equals(alumno)).forEach(calificacionDAO::delete);
    }
}

  • Please show code of `CalificacionRepositorio `, problem might be there. – Ashish Patil May 23 '22 at 17:35
  • Can't really see much without the actual exception – Nico Van Belle May 23 '22 at 17:36
  • 1
    The `AlumnoListener` is managed by JPA/Hibernate not Spring. It has to be a spring component to be able to get autowiring to work. Depending on the version of Spring Boot and Hibernate (I assume) in use that might work (or not if the versions are too old). – M. Deinum May 23 '22 at 17:55

1 Answers1

-2

You probably have to create a bean in your applicationContext.xml

<bean id="CalificacionRepositorio" class="com.example.CalificacionRepositorio" autowire="byName"></bean>

Or programmatically in a @Configuration annotated class within your ComponentScan package

@Bean
@Lazy
public CalificacionRepositorio configuraCalificacionRepositorio() {
    return new CalificacionRepositorio();
}

If you have already registered your bean in the application context make sure that the variable name matches the class name. Try replacing calificacionDAO with calificacionRepositorio

RicardoE
  • 1,665
  • 6
  • 24
  • 42