SpringBoot in @EqualsAndHashCode annotations and notes @Data

       @EqualsAndHashCode notes will generate equals (Object other) and hashCode () methods. It uses the non-static, non-transient property defaults. Some parameters can be exclude excluded properties, may be of only the attributes which specify the parameters. It default method and do not call the parent class using the properties defined in the class, you may be allowed by callSuper = true parent class method calls generated in the process.

       @Data notes equivalent to the set @ Getter, @ Setter, @ RequiredArgsConstructor , @ ToString and @EqualsAndHashCode five annotations. When @Data notes, the notes have @EqualsAndHashCode, it will have a equals (Object other) and hashCode () method in this class, and does not use the property of the parent class, which is causing the problem. For example, a plurality of the same class have some properties, they define the parent class, are just the primary key in the parent class, it will have a portion of the subject at the time of comparison, they are not equal, but because equals (Object lombok automatically generated other) and hashCode () method is determined to be equal, thereby causing an error.
       The solution to this problem is simple: When using @Data notes at the same time plus @EqualsAndHashCode (callSuper = true) annotation. E.g:

@Data
@EqualsAndHashCode(callSuper = true)
public class UserVO extends User {
	@ApiModelProperty(value = "statusName")
	private String statusName;

	@ApiModelProperty(value = "sexName")
	private String sexName;

	@ApiModelProperty(value = "shortName")
	private String shortName;

	@ApiModelProperty(value = "code")
	private String code;
}

 

Published 258 original articles · won praise 1227 · Views 520,000 +

Guess you like

Origin blog.csdn.net/gdkyxy2013/article/details/104769897