I have the following @Builder
- annotated class:
@Data
@Builder(access = AccessLevel.PUBLIC)
@Entity
public class LiteProduct
{
// Minimal information required by our application.
@Id
private Long id; // Unique
private String name;
private Long costInCents;
private String type;
// Model the product types as a Hash Set in case we end up with several
// and need fast retrieval.
public final static Set<String> PRODUCT_TYPES = new HashSet<>
(Arrays.asList("flower", "topical", "vaporizer", "edible", "pet"));
// Have to allow creation of products without args for Entities.
public LiteProduct()
{
}
public LiteProduct(@NonNull final Long id, @NonNull final String name,
@NonNull final String type, @NonNull final Long costInCents)
{
if(!PRODUCT_TYPES.contains(type))
{
throw new InvalidProductTypeException(type);
}
this.name = name;
this.id = id;
this.costInCents = costInCents;
}
Whenever I want to use the builder class that Lombok
is purported to give me, despite the fact that the IDE seems to detect it just fine:
I get a compile-time error about its visibility:
I have looked at some workarounds such as this or this, and they all seem to imply that my problem ought to already be solved automatically and that Lombok
by default produces public
Builder classes. This does not seem to be implied from my output, and does not happen even after I put the parameter access=AccessLevel.PUBLIC
in my @Builder
annotation in LiteProduct
. Any ideas? Is there something wrong with the fact that this class is also an @Entity
? Something else I'm not detecting?
// Edit: I determined that when I move the class in the same package as the one I am calling the builder pattern from, it works just fine. This is not an @Entity
issue, but a package visibility issue which based on what I'm reading should not be there.
The problem was that I was using the following line of code to create an instance of
LiteProduct
:instead of:
which is what the
@Builder
annotation allows you to do. Clearlybuilder()
is like a factory method forBuilder
s that already callsnew
for you.