英文:
Include only one property of nested object to toString method using Lombok
问题
我有以下情况:
@Data
public class Human {
   private long id;
   private Pet pet;
   //其他属性...
}
@Data
public class Pet {
   private long id;
   private Human owner;
   //其他属性...
}
我使用Lombok依赖项,当使用toString方法时,应用程序抛出java.lang.StackOverflowError异常。我知道我可以使用@ToString.Exclude来排除特定字段,但在这种情况下,我希望在toString方法中只显示嵌套对象的id属性。
换句话说,如果对象的toString方法调用其嵌套属性的toString方法,那么(仅在那种情况下)我希望只看到该对象的id。但如果我不是从父对象调用该对象的toString方法,我希望看到常规的toString。
我知道我可以自己定义toString、equals和hashCode方法来实现这一点,但是否可以告诉Lombok来完成这个任务?
英文:
i have a following case:
@Data
public class Human{
   private long id;
   private Pet pet;
   //other properties...
}
@Data
public class Pet{
   private long id;
   private Human owner;
   //other properties...
}
I use lombok dependency and when toString is used, application throws java.lang.StackOverflowError. I know I can use @ToString.Exclude to exclude given field, but in such case i would like to have only a nested object's id proeprty to be shown in a toString method.<br>
In other words, if an object's toString calls it's nested property toString, then (and only then) I want to see only that object id. But if i call that's object toString not from a parent object, I want to see a regular toString. <br>
I know that i can define toString, equals and hashCode methods by myself this way, but is this possible to tell lombok to do that?
答案1
得分: 2
一种方法是为宠物/主人的ID添加一个getter方法,并将其标记为@ToString.Include:
@Data
class Human{
    private long id;
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private Pet pet;
    @ToString.Include
    public long petId() { return pet.getId(); }
    //其他属性...
}
@Data
class Pet{
    private long id;
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private Human owner;
    @ToString.Include
    public long ownerId() { return owner.getId(); }
}
用法:
var human = new Human();
var pet = new Pet();
human.setPet(pet);
pet.setOwner(human);
System.out.println(human);
// 输出:
// Human(id=0, petId=0)
注意,您可能还希望将pet和owner标记为@EqualsAndHashCode.Exclude,因为否则在某些情况下,equals和hashCode也可能导致堆栈溢出。
英文:
One way would be to add a getter for the pet/owner IDs, and mark that as @ToString.Include:
@Data
class Human{
    private long id;
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private Pet pet;
    @ToString.Include
    public long petId() { return pet.getId(); }
    //other properties...
}
@Data
class Pet{
    private long id;
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private Human owner;
    @ToString.Include
    public long ownerId() { return owner.getId(); }
}
Usage:
var human = new Human();
var pet = new Pet();
human.setPet(pet);
pet.setOwner(human);
System.out.println(human);
// Output:
// Human(id=0, petId=0)
Note that you might also want to mark pet and owner as @EqualsAndHashCode.Exclude, because otherwise equals and hashCode might also cause stack overflows in some situations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论