java error : constructor is already defined in class - using lombok
java error : constructor is already defined in class - using lombok
错误
java: constructor Restaurant() is already defined in class
com.example.order_system.domain.Restaurant
当我添加此 class 和 运行 程序时出现
@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<>();
}
尝试将@RequiredArgsConstructor 更改为@AllArgsConstructor,这很好。
在 documentation
中查看更多信息
如 documentation 中所述,@RequiredArgsConstructor 是使用 final
个字段构建的:
@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.
因此,要么删除@RequiredArgsConstructor 注释,要么用final
关键字(或@NonNull 注释)标记一些字段。
错误
java: constructor Restaurant() is already defined in class
com.example.order_system.domain.Restaurant
当我添加此 class 和 运行 程序时出现
@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<>();
}
尝试将@RequiredArgsConstructor 更改为@AllArgsConstructor,这很好。 在 documentation
中查看更多信息如 documentation 中所述,@RequiredArgsConstructor 是使用 final
个字段构建的:
@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.
因此,要么删除@RequiredArgsConstructor 注释,要么用final
关键字(或@NonNull 注释)标记一些字段。