@Column Invalid on Association Fields

ID

java.jpa_association_with_column

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

hibernate, jpa, orm

Description

Reports fields annotated with both a single-valued association (@ManyToOne or @OneToOne) and @Column. The @Column annotation is designed for basic attribute mappings and has no effect on association fields, where the foreign key column should be configured via @JoinColumn instead.

Rationale

Applying @Column to an association field is a common mistake when developers intend to control the foreign key column name. The persistence provider either ignores @Column silently or raises a mapping error, depending on the implementation. Using @JoinColumn is the correct approach for association column configuration.

// Bad: @Column on an association field
@Entity
public class Order {
    @ManyToOne
    @Column(name = "customer_id")  // wrong annotation
    private Customer customer;
}

Remediation

Replace @Column with @JoinColumn on association fields.

// Good: @JoinColumn on an association field
@Entity
public class Order {
    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
}