my problem is that when I'm trying to use @RepositoryEventHandeler annotation I get "Cannot resolve symbol 'RepositoryEventHandler'" information, as if Spring didn't recognize this annotation, I checked and it does not look like I need to add any dependencies for it to work. It's my first attempt at using it so maybe I got the whole idea behind it wrong. What am I doing wrong? Thanks in advance.
Configuration class where I create a bean from class annotated with @RepositoryEventHandler
@Configuration
public class ConfigurationBeans {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AbsenceRepositoryEventHandler absenceRepositoryEventHandler() {
return new AbsenceRepositoryEventHandler();
}
}
Repository
@Repository
public interface AbsenceRepository extends JpaRepository<Absence, Long> {
List<Absence> findAbsencesByBarberId(Long barberId);
List<Absence> findAbsencesByWorkDay_Id(Long workDayId);
}
Entity
@Getter
@Entity
@Table(name = "absences")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Absence {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@ManyToOne
@NotNull
@JoinColumn(name = "barber_id")
private Barber barber;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "work_day_id")
private WorkDay workDay;
@NotNull
@Column(name = "absence_start")
private LocalTime absenceStart;
@NotNull
@Column(name = "absence_end")
private LocalTime absenceEnd;
}
Class annotated with @RepositoryEventHandler (this annotation is all red and gives Cannot resolve symbol 'RepositoryEventHandle info)
@RepositoryEventHandler(Absence.class)
public class AbsenceRepositoryEventHandler {
@HandleBeforeCreate
public void handleAbsenceBeforeCreate(Absence absence){
}
}
Controller class
@RestController
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:3000")
public class AbsenceController {
private final AbsenceServiceImpl absenceService;
private final AbsenceRepository absenceRepository;
@GetMapping("/absences")
public List<Absence> getAllAbsences() {
return absenceRepository.findAll();
}
@GetMapping("/absencesById")
public AbsenceDto getAbsencesById(@RequestParam Long id) {
return absenceService.getAbsenceById(id);
}
@GetMapping("/absencesByBarber")
public List<AbsenceDto> getAbsencesByBarber(@RequestParam Long id) {
return absenceService.getAbsenceByBarber(id);
}
@GetMapping("/absencesByWorkDay")
public List<AbsenceDto> getAbsencesByWorkDay(@RequestParam Long id) {
return absenceService.getAbsenceByWorkDay(id);
}
@PostMapping("/absence")
public AbsenceDto createAbsence(@RequestBody @Valid CreateAbsenceDto absenceDto) {
return absenceService.addAbsence(absenceDto);
}
@PutMapping("/update/absence/{id}")
public ResponseEntity<String> updateAbsence(@PathVariable("id") long id, @RequestBody @Valid AbsenceDto absence) {
absenceService.updateAbsence(id, absence);
return new ResponseEntity<>("Absence was updated.", HttpStatus.OK);
}
@DeleteMapping("/delete/absence/{id}")
public ResponseEntity<String> deleteAbsence(@PathVariable("id") long id) {
absenceService.removeAbsence(id);
return new ResponseEntity<>("Absence was deleted.", HttpStatus.OK);
}
}
Got it. Should've added dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<version>3.0.1</version>
</dependency>
Now it works.