0

I have just added Lombok dependency to my SpringBoot project so I don't have to repeat lines of getters, setters and constructors...

Here is my code example:

Book.java

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "Books")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String title;
    private String ISBN;
    private String author;
    private String issuer;
    private Integer dateOfIssue;
    private Boolean IsRented;

}

But now in my BookService.java I have all the getters and setters turned red with error saying

Cannot resolve method 'setTitle' in 'Book'

This is how I am trying to use getters and setters:

public Book updateBook(Book book){
        Book existingBook = bookRepo.findById(book.getId()).orElse(null);

        existingBook.setTitle(book.getTitle());
        existingBook.setISBN(book.getISBN());
        existingBook.setAuthor(book.getAuthor());
        existingBook.setIssuer(book.getIssuer());
        existingBook.setDateOfIssue(book.getDateOfIssue());
        existingBook.setDateOfIssue(book.getDateOfIssue());
        existingBook.setRented(book.getRented());

        return bookRepo.save(existingBook);
    }

Why is that happening? When I had my getters and setters written like:

 public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

everything was okay but as I removed that and added Lombok it seems like I can't reach to my getters and setters.

dariosicily
  • 4,239
  • 2
  • 11
  • 17
user9347049
  • 1,927
  • 3
  • 27
  • 66

2 Answers2

3

It seems you are using an IDE. For your IDE to recognize the code autogenerated by Lombok you need to install it on your IDE as well as having it as a dependency of your project. The web site for Lombok has instructions on how to do this. For example, if you are using Eclipse, the instructions are here.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
  • 1
    And if using intellij: https://projectlombok.org/setup/intellij – pedrohreis Aug 27 '20 at 14:02
  • Hi mate thank you that was it! I am working on Mac and I had to go to `Intelij IDE > Preferences > Build, Execution, Deployment > Annotation Processors and Enable annotation processing`. `Then also to Preferences > plugins > Lombok Plugin > Install` and it works just fine. – user9347049 Aug 27 '20 at 14:12
  • Thank you for useful link @pedrohreis I am using InteliJ IDE – user9347049 Aug 27 '20 at 14:13
1

You need to install it in your IDE as well. Find the link below for steps

https://projectlombok.org/setup/eclipse

Once you install it it will show as

enter image description here

SSK
  • 3,444
  • 6
  • 32
  • 59