Lombok @Builder and JPA Default constructor

前端 未结 8 1903
遇见更好的自我
遇见更好的自我 2020-12-04 11:03

I\'m using project Lombok together with Spring Data JPA. Is there any way to connect Lombok @Builder with JPA default constructor?

Code:



        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 11:21

    If the annotations lombok.Tolerate on constructor and javax.validation.constraints.NotNull on some property are used at the same time, sonarqube will mark it as a critical error: PROPERTY is marked "javax.validation.constraints.NotNull" but is not initialized in this constructor.

    If the project uses SpringData with JPA, it can be solved using org.springframework.data.annotation.PersistenceConstructor (Spring annotation, not JPA!)

    Then, in combination with Lombok, annotations will be like this:

    @RequiredArgsConstructor(onConstructor = @__(@PersistenceConstructor))
    

    For Lombok builder you also need to add:

    @Builder
    @AllArgsConstructor
    

提交回复
热议问题