Lombok用@ ToString.Exclude排除字段不起作用 [英] Lombok excluding field with @ToString.Exclude is not working

查看:136
本文介绍了Lombok用@ ToString.Exclude排除字段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Lombok删除样板代码.我正在尝试将实体打印到控制台,但出现StackOverflowError.该实体与另一个实体具有双向关系,因此我想将此实体从toString方法中排除.

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; 
}

这是我第一次尝试使用 @ ToString.Exclude ,它似乎没有表现.我使用不正确吗?当我在Foo对象上调用toString时,我只想打印出 fooId name .

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.

修改

我熟悉从顶级 @ToString 注释中排除或包含字段的替代方法.我正试图避免这种情况.我只想在类级别使用 @Data ,并注释应排除的字段.

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.

编辑2

仍然在一个简化的类上复制.龙目岛1.18.8版.

Still replicating on a simplified class. Lombok version 1.18.8.

推荐答案

为我工作. 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";

}

输出: ToString :: MyClass(a = ABC)

这篇关于Lombok用@ ToString.Exclude排除字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆