10

The error

java: constructor Restaurant() is already defined in class  
com.example.order_system.domain.Restaurant

appear when I add this class and run the program

@Entity
@NoArgsConstructor
@RequiredArgsConstructor
@Getter
@Setter
@ToString
public class Restaurant {

    @Id
    @GeneratedValue
    private long id;

    @NotEmpty(message = "The restaurant must have a name")
    private String name;

    @NotEmpty(message = "Please add a description for this restaurant")
    private String description;

    @NotEmpty(message = "The restaurant must have a location")
    private String location;

    @OneToMany(mappedBy = "restaurant", fetch = FetchType.EAGER)
    private List<ContactDetails> contactDetails = new ArrayList<>();

}
4
  • You might have another Restaurant constructor with the same signature, perhaps inside another package. Commented Feb 21, 2021 at 11:23
  • 1
    @RequiredArgsConstructor checks for uninitialized final and @NotNull fields, not for @NotEmpty ones, so it's probably just generating another no-arguments constructor. Commented Feb 21, 2021 at 11:25
  • 1
    @ASH thanks for your answer, but the correct answer is as it explained in the answers section below Commented Feb 21, 2021 at 11:38
  • In this case you may want to use the @Data annotation anyway. It combines @RequiredArgsConstructor, @Getter, @Setter, @ToString and additionally @EqualsAndHashCode which is always a good idea for entities. Commented Feb 21, 2021 at 11:47

4 Answers 4

19

As mentioned in documentation @RequiredArgsConstructor is built using final fields:

@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared. For those fields marked with @NonNull, an explicit null check is also generated. The constructor will throw a NullPointerException if any of the parameters intended for the fields marked with @NonNull contain null. The order of the parameters match the order in which the fields appear in your class.

So either remove @RequiredArgsConstructor annotation or mark some of fields with final keyword (or @NonNull annotation).

Sign up to request clarification or add additional context in comments.

Comments

5

Try to change @RequiredArgsConstructor to @AllArgsConstructor and that's been fine. See more in documentation

Comments

1

In my scenario, an empty class was present throwing the same above error, removing all the constructor based annotations i.e. @AllArgsConstructor and @NoArgsConstructor, solved the issue for me.

Comments

0

Another scenario that causes this error (even with newer Lombok versions) is when running tests in IntelliJ while using its "Annotation processors", which seems to have its own logic that doesn't handle duplicates quite so nicely.

You can fix it with the same workaround of removing one of the conflicting @NoArgsConstructor or @AllArgsConstructor annotations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.