在@Data注释lombok上使用继承警告等于/ hashCode [英] Warning equals/hashCode on @Data annotation lombok with inheritance
问题描述
我有一个继承自其他的实体。另一方面,我使用lombok项目来减少样板代码,所以我把 @Data
注释。带继承的注释 @Data
会产生下一个警告:
I have a entity which inherits from other. On other hand, I'm using lombok project to reduce boilerplate code, so I put @Data
annotation. The annotation @Data
with inheritance produces the next warning:
生成equals / hashCode实现但没有调用超类,即使这个类没有扩展java.lang.Object。如果这是故意的,请将
@EqualsAndHashCode(callSuper = false)
添加到您的类型。
建议添加注释 @EqualsAndHashCode(callSuper = true)
或 @EqualsAndHashCode(callSuper = false)
?如果没有添加,那么 callSuper = false
或 callSuper = true
?
Is it advisable to add annotation @EqualsAndHashCode (callSuper = true)
or @EqualsAndHashCode (callSuper = false)
? If it is not added, Which one is it callSuper=false
or callSuper=true
?
推荐答案
默认值是 false
。如果您没有指定它并忽略警告,那就是您获得的那个。
The default value is false
. That is the one you get if you don't specify it and ignore the warning.
是的,建议添加 @EqualsAndHashCode
注释 @Data
带注释的类,它们扩展了除Object以外的其他类。我不能告诉你是否需要 true
或 false
,这取决于你的类层次结构,需要进行检查根据具体情况而定。
Yes, it is recommended to add an @EqualsAndHashCode
annotation on the @Data
annotated classes that extend something else than Object. I cannot tell you if you need true
or false
, that depends on your class hierarchy, and will need to be examined on a case-by-case basis.
但是,对于项目或包,您可以在中配置lombok.config
如果它不是Object的直接子类,则调用super方法。
However, for a project or package, you can configure in lombok.config
to call the super methods if it is not a direct subclass of Object.
lombok.equalsAndHashCode.callSuper = call
请参阅配置系统文档以及 @支持配置密钥的EqualsEndHashCode
文档。
See the configuration system documentation on how this works, and the @EqualsEndHashCode
documentation for the supported configuration keys.
披露:我是一名lombok开发人员。
Disclosure: I am a lombok developer.
这篇关于在@Data注释lombok上使用继承警告等于/ hashCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!