Lombok builder() 方法在 Spring Boot 应用程序中返回 null

问题描述 投票:0回答:1

我正在使用 JPA 和 Lombok 开发 Spring Boot 应用程序。我有一个带有 Lombok 注释的作者实体。但是,当我尝试使用 builder() 方法创建 Author 对象时,IntelliJ IDEA Community Edition 将 builder() 方法显示为红色,并且在运行时返回 null。

JpaApplication.java

package com.explorer.jpa;

import com.explorer.jpa.models.Author;
import com.explorer.jpa.repositories.AuthorRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(AuthorRepository repository) {
        return args -> {
            var author = Author.builder()  // This line shows in red in IntelliJ, Exactly the .builder() part
                    .firstName("Johnny")
                    .lastName("Deep")
                    .age(35)
                    .email("[email protected]")
                    .build();

            repository.save(author);
        };
    }
}

作者.java

package com.explorer.jpa.models;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class Author {
    @Id
    @GeneratedValue
    private Integer id;

    @Column(name = "firstname", length = 50)
    private String firstName;

    @Column(name = "lastname")
    private String lastName;

    @Column(unique = true, nullable = false)
    private String email;

    private int age;

    @Column(updatable = false, nullable = false)
    private LocalDateTime createdAt;

    @Column(insertable = false)
    private LocalDateTime lastModified;
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.explorer</groupId>
    <artifactId>jpa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jpa</name>
    <description>Demo project for Spring Boot JPA</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

龙目岛配置:

  • Lombok 插件已在 IntelliJ IDEA 中安装并启用。
  • 在 IntelliJ 设置中启用注释处理。

清理和重建项目:

  • 运行 mvn clean install。
  • 在 IntelliJ 中重建项目。

验证Lombok注释:

  • 确保 Author 类具有正确的 Lombok 注释。

检查日志并调试:

  • 日志中没有与 Lombok 或 builder() 方法相关的具体错误消息。
  • 尽管采取了这些步骤,问题仍然存在。任何有关解决此问题的帮助将不胜感激!

其他详细信息:

  • IDE:IntelliJ IDEA 社区版
  • Java版本:17
  • Spring Boot版本:3.3.1
java spring-boot spring-mvc spring-data-jpa lombok
1个回答
0
投票
import java.time.LocalDateTime;
import javax.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class Author {

public Author (Integer id,String firstName,String lastName,String email, int 
age){
this.id = id;
this.firstName= firstName;
this.lastName= lastName;
this.email = email;
this.age = age;


}


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

@Column(name = "firstname", length = 50)
private String firstName;

@Column(name = "lastname")
private String lastName;

@Column(unique = true, nullable = false)
private String email;

private int age;

@Column(updatable = false, nullable = false)
private LocalDateTime createdAt;

@Column(insertable = false)
private LocalDateTime lastModified;

@PrePersist
protected void onCreate() {
    createdAt = LocalDateTime.now();
}

@PreUpdate
protected void onUpdate() {
    lastModified = LocalDateTime.now();
  }
}
//Use above code .I add constructor with the parameter
© www.soinside.com 2019 - 2024. All rights reserved.