0

Fields in a "Serializable" class should either be transient or serializable is possible to fix any entity/class is used in another class, but it occurs when List/ Map is declared in a dto class which can't be even made as transient as well. please let me know how to fix this.

Ex:

public class CustomMetadataDTO implements Serializable {

private UUID id;

private Map<String, Object> metadata = new HashMap<>();

private UUID fieldGroupId;

private Integer order;


public CustomMetadataDTO(Map<String, Object> metadata, Integer order) {
    this.metadata = metadata;
    this.order = order;
}

public CustomMetadataDTO() {
}}

for below line I get the sonarqube issue as critical

private Map<String, Object> metadata = new HashMap<>();

enter image description here

Thomson Ignesious
  • 689
  • 1
  • 8
  • 25
  • 1
    Possible duplicate of [Fields in a "Serializable" class should either be transient or serializable](https://stackoverflow.com/questions/49632332/fields-in-a-serializable-class-should-either-be-transient-or-serializable) – Kavitha Karunakaran Nov 20 '19 at 07:32
  • 1
    The objects stored in the map aren't serializable, you should at least set the type instead of Object to Serializable. – Slimu Nov 20 '19 at 07:38

1 Answers1

1

HashMap is serializable but Map is not as it does not implement Serializable interface. You can try using HashMap itself while declaring (not recommended).

private HashMap<String, Object> metadata = new HashMap<>();
Jerin D Joy
  • 750
  • 6
  • 11