BadPractice5 - SpotBugsExtensionForSpringFrameWork/CS5098 GitHub Wiki

Bug Pattern Type: INEFFICIENT_ORDINAL_ENUM_MAPPING Short Description: Use @Enumerated(EnumType.STRING) over @Enumerated(EnumType.ORDINAL)

Description & Theory

A problem with this kind of mapping arises when we need to modify our enum. If we add a new value in the middle or rearrange the enum's order, we'll break the existing data model. Such issues might be hard to catch, as well as problematic to fix, as we would have to update all the database records.

@Entity
public class Article {
    @Id
    private int id;

    private String title;

    @Enumerated(EnumType.ORDINAL) // Better not to use this
    private Status status;
}

Solution

@Entity
public class Article {
    @Id
    private int id;

    private String title;

    @Enumerated(EnumType.STRING) // ORDINAL to STRING 
    private Status status;
}

Reference List

https://www.baeldung.com/jpa-persisting-enums-in-jpa#ordinal https://stackoverflow.com/questions/6789342/jpa-enum-ordinal-vs-string