有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在spring JPA保存方法中添加用户定义注释

如何在spring jpa save method only中添加用户定义的注释

我已经创建了一个注释,并希望在存储库的save方法上使用它,但save方法是从JPA的Crudepository继承的方法,不确定如何仅在该存储库的该方法而不是其他方法上应用注释

尝试在repository接口中重写该方法并应用注释,但没有成功

请参考下面的代码-

注释:

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

@Aspect
@Configuration
@Slf4j
@ComponentScan(value = "com.somepackage.service")
public class MyAnnotationInterceptor {

   @Value("${val}")
   private String val;

   @Around("@annotation(com.somepackage.service.application.annotation.MyAnnotation)")
   public void around(ProceedingJoinPoint joinPoint) throws Throwable {
      if("TEST".equalsIgnoreCase(val)){
         log.info("Test Event")
      }else{
         joinPoint.proceed();
      }
   }
}

存储库:

@Transactional
public interface EmployeeEntityRepository extends CrudRepository<EmployeeEntity, String> {

   List<EmployeeEntity> findAllByEmpIdAndStatusNot(String empId, String status);

   @Query("SELECT emp.empId FROM EmployeeEntity emp WHERE emp.orderId IN ?1")
   List<String> findEmployeeIds(List<String> orderIds);

   @Override
   @MyAnnotation
   <S extends EmployeeEntity> Iterable<S> save(Iterable<S> iterable);
}

服务类别:

class EmployeeService {

    @Autowired
    EmployeeEntityRepository employeeEntityRepo;

    public void saveEmployee(List<EmployeeEntity> employeeData) {
        employeeEntityRepo.save(employeeData);
        employeeEntityRepo.clearCache(employeeData);
        /***
        .
        .
        .
        Some other logic calculations 
        .
        .
        ***/
    }

}

共 (0) 个答案