6

I'm using lombok project with Entity here is my example :

package com.company.entities;//<---------Note the package 

import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.Builder; 
import lombok.Getter; 
import lombok.Setter;

@Entity
@Builder
@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
public class Client {

    @Id
    private long id;
    private String firstName;
    private String lastName;

}

So when I try to use in the same package, It works fine :

When I change the package for example to package com.company.controllers; :

package com.company.controllers;//<---------Note the package 

public class Mcve {

    public static void main(String[] args) {
        Client client = new Client.ClientBuilder()
                .id(123)
                .firstName("firstName")
                .lastName("lastName")
                .build();
    }     
}

I get an error :

ClientBuilder() is not public in com.company.entities.Client.ClientBuilder; cannot be accessed from outside package

I tried all the solution in this posts :

I test with lombok 1.16.18 and 1.16.20.

When I create my own builder class it works fine, but when I use @Builder it not, I know what does this mean, but no way, I can't arrive to solve this issue ! what should I do to solve this problem?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

1 Answers1

20

You don't have to instantiate the builder. Instead use:

  Client client = Client.builder()
            .id(123)
            .firstName("firstName")
            .lastName("lastName")
            .build();
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • bruh thank you!! I was trying to do "new" builder but this solves the access problem... there is an army out there struggling with this issue after much searching, I'm concerned for those not seeing this answer, they are getting too creative – onionring May 17 '23 at 04:32