How to implement Lombok in the Spring Boot project

Asked

Viewed 1,669 times

0

I’m in trouble in this class, take a look

@Service
public class CadastroEstiloService {

    @Autowired
    private Estilos estilos;

    @Transactional
    public void salvar(Estilo estilo) {
        Optional<Estilo> estiloOptional = estilos.findByNomeIgnoreCase(estilo.getNome());
        if (estiloOptional.isPresent()) {
            throw new NomeEstiloJaCadastradoException("Nome do estilo já cadastrado");
        }

        estilos.save(estilo);
    }

}

You’re not recognizing the getName after I changed my entity to implement Lombok

check my entity before the changes;

@Entity
@Table(name = "estilo")
public class Estilo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long codigo;

    @NotBlank(message = "O nome é obrigatório")
    @Size(max = 20, message = "O tamanho do nome não pode ser maior que {max} caracteres")
    private String nome;

    @OneToMany(mappedBy = "estilo")
    private List<Cerveja> cervejas;

    public Long getCodigo() {
        return codigo;
    }

    public void setCodigo(Long codigo) {
        this.codigo = codigo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Estilo other = (Estilo) obj;
        if (codigo == null) {
            if (other.codigo != null)
                return false;
        } else if (!codigo.equals(other.codigo))
            return false;
        return true;
    }

}

My entity after the changes;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "estilo")
@EqualsAndHashCode
public class Estilo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter @Setter
    private Long codigo;

    @NotBlank(message = "O nome é obrigatório")
    @Size(max = 20, message = "O tamanho do nome não pode ser maior que {max} caracteres")
    @Getter @Setter
    private String nome;

    @OneToMany(mappedBy = "estilo")
    @Getter @Setter
    private List<Cerveja> cervejas;


}

i follow this site for me to guide CLICK HERE

I accept suggestions!

I added Lombok through Maven;

         <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
  • The getters and setters ("getName", for example) will be generated during the build of the project. Enable the IDE’s automatic build (Eclipse, Netbeans...) and by including annotations save all changed files before you wait for them getters and setters are recognised. Also try a clean/build project - automatic build of the IDE may have failed or been incomplete on the first attempt.

  • Did you install Lombok in your IDE as the tutorial teaches? Which IDE do you use?

  • i added the Lombok through the Maven, it managed to get the version Lombok 1.16.12, detail, my project is already with build enabled and I closed and opened the project and gave a clear in the project and nothing of result.

  • I’m using the Eclipse IDE

  • staff still need help!

  • Have you already installed/configured the Lombok plugin in your IDE? Try building with Maven(mvn compile), and provide us more details on the error, a stacktrace would be interesting.

  • Do not forget that I am working with Maven, I include the artifact in the file pom.xml then saved and gave clear in the project, Eclipse is with the build in automatic, even after I have done this, I right-clicked on the mouse in the project I went in Maven -> Update Project, and still not solved. The project doesn’t generate error messages. What I can do is create a simple project that implements Lombok and put it into Github to analyze.

  • Don’t just put the Maven dependency. You need to install Lombok TOO in the IDE.

Show 3 more comments

1 answer

1


Try adding annotations: @Getter @Setter

@Getter @Setter @Entity @Table(name = "style") public class Style Implements Serializable {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.