@Entity with @AllArgsConstructor Missing @NoArgsConstructor

ID

java.jpa_allargsconstructor_without_noargs

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 @AllArgsConstructor but not @NoArgsConstructor. Lombok’s @AllArgsConstructor generates a constructor with all fields as parameters, which suppresses the compiler-generated default no-arg constructor. JPA requires a no-arg constructor for proxy creation and entity instantiation.

Rationale

When any explicit constructor is declared (or generated by Lombok), the Java compiler no longer provides the implicit no-arg constructor. JPA mandates a no-arg constructor (public or protected) so that the persistence provider can instantiate entities via reflection. Without it, the application fails at runtime with an InstantiationException.

// Bad: @AllArgsConstructor suppresses default no-arg constructor
@Entity
@AllArgsConstructor
public class Customer {
    @Id
    private Long id;
    private String name;
}

Remediation

Add @NoArgsConstructor alongside @AllArgsConstructor on JPA entity classes.

// Good: both constructor annotations present
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Customer {
    @Id
    private Long id;
    private String name;
}