Lombok excluding field with @ToString.Exclude is not working

心已入冬 提交于 2020-08-06 09:37:21

问题


I'm using Lombok to remove boilerplate code. I'm attempting to print out an entity to the console but I'm getting a StackOverflowError. The entity has a bidirectional relationship with another entity, so I want to exclude this entity from the toString method.

My entity looks like this:

@Entity
@Data
public class Foo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long fooId;

    private String name;

    @ManyToOne
    @JoinColumn(name = "barId")
    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    private Bar bar; 
}

This is my first time attempting to use @ToString.Exclude and it doesn't appear to be behaving. Am I using it incorrectly? I just want to print out fooId and name when I call toString on the Foo object.

Edit

I'm familiar with alternate approaches to excluding or including fields from a top level @ToString annotation. I'm attempting to avoid that. I just want to use @Data at the class level, and annotate the fields that should be excluded.

Edit 2

Still replicating on a simplified class. Lombok version 1.18.8.


回答1:


Works for me. lombok:1.18.8

import lombok.Data;
import lombok.ToString;

@Data
public class MyClass {
    public static void main(String args[]) {
        MyClass myClass = new MyClass();
        System.out.println("ToString::" + myClass);
    }

    private String a = "ABC";

    @ToString.Exclude
    private String b = "DEF";

}

Output: ToString::MyClass(a=ABC)




回答2:


You will have to update the lombok installed in your eclipse (download the new lombok.jar, run java -jar lombok.jar and restart Eclipse). At least that solved it for me.




回答3:


In Kotlin

You could avoid using the Lombok library and define a custom annotation @ExcludeToString to exclude the redundant parameter or what you need.

GL



来源:https://stackoverflow.com/questions/56263350/lombok-excluding-field-with-tostring-exclude-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!