Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entities with @RequiredArgsConstructor error in IntelliJ IDE

I'm trying to run sample Spring boot application and I'm facing problem with entities that are marked as @RequiredArgsConstructor on my IDE. I'm using latest intelliJ IDEA (14.1) over java 1.8. There's an error marked on IDE when I tried to initialize the entity with constructor arguments.

E.g. It would be showing "cannot resolve symbol" for following line.

itemRepository.save(new Item("MacBook Pro"));

My entity would be as follows.

@Entity
@Data
@RequiredArgsConstructor
public class Item {

    private @Id @GeneratedValue Long id;
    private final String description;

    Item() {
        this.description = null;
    }
}

Apart from IDE error project builds and runs properly.

like image 231
Charith De Silva Avatar asked Sep 20 '15 00:09

Charith De Silva


People also ask

What is required args constructor?

@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.


1 Answers

The sample project you are running uses Lombok, a library which can generate a lot of boilerplate code for you (such as getters and setters) based on annotations (e.g. @RequiredArgsConstructor). This is useful, but because the code is generated during compilation, the IDE doesn't see it and therefore shows errors.

You must install Lombok plugin to make IntelliJ aware that the constructor actually does exist, but is generated during compilation. Then the errors will go away.

You can also take a look at this post for more details about how Lombok works under the hood.

like image 178
Bohuslav Burghardt Avatar answered Sep 19 '22 21:09

Bohuslav Burghardt