Overflow when deserializing relational database objects Medium

Deserializing objects from relational databases should allocate a 64-bit, not 32-bit, type for the auto-incremented identifier. Otherwise, overflow is possible which can cause unintended deserialization behavior.

Detector ID
java/javax-persistence-id@v1.0
Category

Noncompliant example

1@Entity
2@Data
3@Builder
4@NoArgsConstructor
5@AllArgsConstructor
6public class JavaxPersistenceIdNoncompliant {
7
8    // Noncompliant: attaches an auto-incremented ID to 32-bit data
9    @Id
10    @GeneratedValue(strategy = GenerationType.IDENTITY)
11    @Column(name = "role_sla_id", nullable = false)
12    private Integer id;
13
14    @Column(name = "type_id", nullable = false)
15    private Integer typeId;
16
17    @Column(name = "type", nullable = false, length = 255)
18    private String type;
19}

Compliant example

1@Entity
2@Data
3@Builder
4@NoArgsConstructor
5@AllArgsConstructor
6@Table(name = "role_sla_type")
7public class JavaxPersistenceIdCompliant {
8
9    // Compliant: does not attach an auto-incremented ID to 32-bit data.
10    @GeneratedValue(strategy = GenerationType.IDENTITY)
11    @Column(name = "role_sla_id", nullable = false)
12    private Integer id;
13
14    @Column(name = "type_id", nullable = false)
15    private Integer typeId;
16
17    @Id
18    @Column(name = "type", nullable = false, length = 255)
19    private String type;
20}