Nitin Bisht
Nitin Bisht

Reputation: 5341

@RequiredArgsConstructor does not generate constructor

I have a Group class:

@Data
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
public class Group {

    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    private String name;
    private String address;
    private String city;
    private String stateOrProvince;
    private String country;
    private String postalCode;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private User user;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Event> event;

}

A GroupRepository interface:

public interface GroupRepository extends JpaRepository<Group, Long> {

    Group findByName(String name);
}

A Initializer class to load data:

@Component
public class Initializer implements CommandLineRunner {

    private final GroupRepository repository;

    public Initializer(GroupRepository groupRepository) {
        this.repository = groupRepository;
    }

    @Override
    public void run(String... args) throws Exception {
        Stream.of("Denver JUG", "Utah JUG", "Seattle JUG",
                "Richmond JUG").forEach(name ->
                repository.save(new Group(name)));
    }
}

Specifications:

  1. IDE: Eclipse
  2. Java: 1.8

Why I am getting an error on repository.save(new Group(name))); and how to resolve it?

Error: The constructor Group(name) is undefined.

Note: Although I am using lombok and added @NotNull on name field in Group class.

Upvotes: 0

Views: 622

Answers (1)

kaos
kaos

Reputation: 1608

I think Lombok does not suport @NotNull, You need to make field final or use Lombok's @NonNull.

Upvotes: 3

Related Questions