简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM