2017-09-17 93 views
0

我有JPA實體,它擴展了其他抽象類。我想使用@Data來避免編寫setter和getters,但我的equals和hashcode方法存在。Lombok - 在等於和哈希碼時執行@Data警告

我得到警告,但我覺得我不應該:

server\entity\User.java:20: warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type. 
@Data 
^ 

在我的用戶等級:

@Data 
@Entity 
public class User extends AbstractAuditingEntity implements Serializable { 

    .... 

    @Override 
    public boolean equals(Object o) { 
     ... 
    } 

    @Override 
    public int hashCode() { 
     ... 
    } 
} 

當我添加額外@EqualsAndHashCode(callSuper = false)來@Data我得到:

server\entity\User.java:21: warning: Not generating equals and hashCode: A method with one of those names already exists. (Either both or none of these methods will be generated). 
@EqualsAndHashCode(callSuper = false) 
+1

我不想無禮,但消息是很明確的在這裏。你不明白什麼? –

+0

@RC消息是「生成equals/hashCode實現,但沒有對超類的調用」。我的理解是,當方法實施時不會產生,所以這使我感到困惑。 –

+0

@RC。他們是明確的,但行爲似乎是錯誤的。當手動定義'equals'和'hashCode'時,'@ Data'應該在沒有任何警告的情況下跳過它們。 – maaartinus

回答

4

@Data@ToString,快捷方式210,@Getter@Setter@RequiredArgsConstructor。因爲你只是想@Getter@Setter,你爲什麼不只是使用它們(這將避免你的異常或警告消息),

@Entity 
@Getter 
@Setter 
public class User extends AbstractAuditingEntity implements Serializable 
    ... 

} 
+0

好點,但我仍然想知道爲什麼@Data的行爲如此。 –

+0

當龍目遇到'@ Data'註釋時,它會嘗試執行'equals'和'hashCode'。在Lombok進行二進制代碼注入的過程中,JVM會抱怨,因爲equals類和hashCode方法已經存在於要修改的類中。 –

+0

@KrzysztofKot你說得對,並在[此評論]的解釋(https://stackoverflow.com/questions/46261314/lombok-warning-with-data-when-equals-and-hashcode-are-implemented#comment79495674_46261362 ) 是錯的。這是一個錯誤:https://github.com/rzwitserloot/lombok/issues/1078 – maaartinus