Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make "data" transient or serializable SonarQube Error

I have created this class

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = { "id" })
@Builder(toBuilder = true)
@JsonInclude(NON_NULL)
public class Hostel<T> implements Serializable {

    @Field
    private Long version;   
    @JsonProperty("_data")
    private @NotNull T data;

}

But I have this SonarQube error:

Make "data" transient or serializable SonarQube Error

but data is a Generic type

like image 776
Sandro Rey Avatar asked Sep 02 '26 01:09

Sandro Rey


2 Answers

You can make it transient, or just bound T with <T extends Serializable>.

like image 135
Kayaman Avatar answered Sep 03 '26 16:09

Kayaman


T is unknown type here. So, it's unpredictable whether T is serializable or not. most of the inbuilt classes of java are serializable e.g. String, Map, List etc but when you use generic types it's unpredictable and hence bound it to be Serializable Or if you don't want it to be serialized then mark it transient.

Read here for more on bounded types

like image 35
Vinay Prajapati Avatar answered Sep 03 '26 16:09

Vinay Prajapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!