@Entity with @Builder Missing Required Constructors

ID

java.jpa_lombok_builder_missing_constructor

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

hibernate, jpa, orm

Description

Reports @Entity classes annotated with Lombok @Builder that are missing either @AllArgsConstructor or @NoArgsConstructor. Lombok’s @Builder generates an all-args constructor, which suppresses the compiler-generated default no-arg constructor that JPA requires. To use both @Builder and JPA together, both constructor annotations must be present.

Rationale

When @Builder is applied, Lombok generates an all-args constructor for the builder to use. This suppresses the default no-arg constructor. JPA requires a no-arg constructor for entity instantiation and proxy creation. Without @NoArgsConstructor, the persistence provider throws an InstantiationException at runtime. Without @AllArgsConstructor, the builder-generated constructor conflicts with the Lombok-generated no-arg constructor.

// Bad: @Builder without both constructor annotations
@Entity
@Builder
public class Order {
    @Id
    private Long id;
    private String description;
}

Remediation

Add both @AllArgsConstructor and @NoArgsConstructor alongside @Builder on JPA entity classes.

// Good: all three Lombok annotations present
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    @Id
    private Long id;
    private String description;
}