Home > front end >  Issue with Lombok not generating getters and setters or constructor and even @Data in Spring Boot pr
Issue with Lombok not generating getters and setters or constructor and even @Data in Spring Boot pr

Time:11-05

I utilized Spring Boot to initialize my project, and indeed, I included the Lombok dependency through Spring Boot and configured the Lombok plugin as follows:

And The IDE I use is Eclipse

<?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.2.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.springbootExample</groupId>
    <artifactId>SpringBoot_Example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot_Example</name>
    <description>Demo project for Spring Boot</description>
    
    <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-validation</artifactId>
        </dependency>
        
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
          <optional>true</optional>
        </dependency>
        
        <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <scope>runtime</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>12.4.2.jre11</version>
        </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>
  
  <repositories>
      
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
    
  </repositories>
  
  <pluginRepositories>
      
    <pluginRepository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
    
    <pluginRepository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </pluginRepository>
    
  </pluginRepositories>

</project>

However, when I use it in my entity class named department.java, it appears that Lombok is not working as expected. Even after annotating the class with @Data, @NoArgsConstructor, @AllArgsConstructor, and @Builder, the issue persists.

package com.springbootExample.SpringBoot_Example.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class department {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long departmentID;
    
    @NotBlank(message = "Please add department name")
//  @Length(max = 20, min = 10)
//  @Size
//  @Email
//  @Positive
//  @Negative
//  @PositiveOrZero
//  @Future
//  @Past
//  @FutureOrPresent
    private String departmentName;
    private String departmentAdress;
    private String departmentCode;
}

In another configuration Java class, I'm also unable to use the getter and setter methods of the department class.

package com.springbootExample.SpringBoot_Example.service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbootExample.SpringBoot_Example.entity.department;
import com.springbootExample.SpringBoot_Example.error.departmentExceptionHandling;
import com.springbootExample.SpringBoot_Example.repository.departmentRepository;

@Service
public class departmentServiceImplement implements departmentService{
    
    @Autowired
    private departmentRepository dr;

    @Override
    public department saveDepartment(department dp) {
        return dr.save(dp);
    }

    @Override
    public List<department> fetchDepartment() {
        return dr.findAll();
    }

    @Override
    public department fetchDepartmentById(Long departmentID) throws departmentExceptionHandling {
        Optional<department> dpt = dr.findById(departmentID);
        
        if(!dpt.isPresent()) {
            throw new departmentExceptionHandling("Department was not found");
        }
        return dpt.get();
    }

    @Override
    public void deleteDepartmentById(Long departmentID) {
        dr.deleteById(departmentID);
        
    }

    @Override
    public department updateDepartment(Long departmentID, department dp) {
        department dep = dr.findById(departmentID).get();
        
        if(Objects.nonNull(dp.getDepartmentName()) && !"".equalsIgnoreCase(dp.getDepartmentName())) {
            dep.setDepartmentName(dp.getDepartmentName());
        }
        
        if(Objects.nonNull(dp.getDepartmentCode()) && !"".equalsIgnoreCase(dp.getDepartmentCode())) {
            dep.setDepartmentCode(dp.getDepartmentCode());
        }
        
        if(Objects.nonNull(dp.getDepartmentAdress()) && !"".equalsIgnoreCase(dp.getDepartmentAdress())) {
            dep.setDepartmentAdress(dp.getDepartmentAdress());
        }
        
        return dr.save(dep);
    }

    @Override
    public department fetchDepartmentByName(String departmentName) {
        return departmentRepository.findByDepartmentNameIgnoreCase(departmentName);
    } 

}

The errors show as

The method getDepartmentName() is undefined for the type department
The method getDepartmentCode() is undefined for the type department
The method getDepartmentAdress() is undefined for the type department

I've tried several solutions, such as updating my IDE, changing Lombok plugins, and installing Lombok, but none of them have resolved the issue.

If anyone has encountered a similar problem and knows how to fix it, I would greatly appreciate your help.

Thank you in advance.

CodePudding user response:

try delete the target folder and run your project again.It's work with me

CodePudding user response:

These annotations are to be executed before compilation. In Gradle, you need to use annotationProcessor <lombok> to achieve this. What you have added is the default scope, which may be runtime or compile (not sure).

For Maven, you will have to use the equivalent of annotationProcessor, which is explained here (in the question itself).

  • Related