为什么我的toString()方法不起作用,如何防止在SpringBootJPA中返回嵌套结果?

huangapple go评论74阅读模式
英文:

Why is my toString() method not working and how to prevent nested returns in SpringBootJPA?

问题

我有两个实体,合作伙伴(Partner)和广告活动(Campaign),因此我已创建了一个“一对多”单向关系。

这是我的 Partner 实体:

@Entity
@Table(name = "partner")
@EntityListeners(AuditingEntityListener.class)
public class Partner {

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;

    @NotNull
    @Column(name = "partner_name")
    private String partnerName;

    @NotNull
    @Column(name = "partner_code")
    private String partnerCode;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "partner_id")
    private List<Campaign> campaigns = new ArrayList<>();

    // 构造方法,getter 和 setter,equals,hashcode

    @Override
    public String toString() {
        return "Partner{" +
                "id='" + id + '\'' +
                ", partnerName='" + partnerName + '\'' +
                ", partnerCode='" + partnerCode + '\'' +
                '}';
    }
}

这是我的 Campaign 实体:

@Entity
@Table(name = "campaign")
@EntityListeners(AuditingEntityListener.class)
public class Campaign {

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;

    @NotNull
    @Column(name = "partner_name")
    private String partnerName;

    @NotNull
    @Column(name = "partner_code")
    private String partnerCode;

    @NotNull
    @Column(name = "campaign_name")
    private String campaignName;

    @NotNull
    @Column(name = "campaign_code")
    private String campaignCode;

    // 构造方法,getter 和 setter,equals,hashcode

    @Override
    public String toString() {
        return "Campaign{" +
                "id='" + id + '\'' +
                ", partnerName='" + partnerName + '\'' +
                ", partnerCode='" + partnerCode + '\'' +
                ", campaignName='" + campaignName + '\'' +
                ", campaignCode='" + campaignCode + '\'' +
                '}';
    }
}

但是,当我调用 findAll() 方法获取合作伙伴时,这是我的响应:

为什么我的toString()方法不起作用,如何防止在SpringBootJPA中返回嵌套结果?

我需要的是在获取 findAll() 合作伙伴方法的响应中不获取 Campaigns 属性。我不明白为什么我的 toString 方法不起作用,如果我已经定义了我只想打印 id、partnerName 和 partnerCode。

因此,我不希望有嵌套的响应。

提前感谢您的帮助。

英文:

I have two Entities, Partner and Campaign, so I have created a "One to many" unidirectional relationship.

This is my Partner entity:

@Entity
@Table(name = &quot;partner&quot;)
@EntityListeners(AuditingEntityListener.class)
public class Partner {

    @Id
    @GeneratedValue(generator = &quot;uuid&quot;)
    @GenericGenerator(name = &quot;uuid&quot;, strategy = &quot;uuid2&quot;)
    private String id;

    @NotNull
    @Column(name = &quot;partner_name&quot;)
    private String partnerName;

    @NotNull
    @Column(name = &quot;partner_code&quot;)
    private String partnerCode;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = &quot;partner_id&quot;)
    private List&lt;Campaign&gt; campaigns = new ArrayList&lt;&gt;();

    // constructos, getters, setters, equals, hashcode

    @Override
    public String toString() {
        return &quot;Partner{&quot; +
                &quot;id=&#39;&quot; + id + &#39;\&#39;&#39; +
                &quot;, partnerName=&#39;&quot; + partnerName + &#39;\&#39;&#39; +
                &quot;, partnerCode=&#39;&quot; + partnerCode + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }

This is my Campaign entity:

@Entity
@Table(name = &quot;campaign&quot;)
@EntityListeners(AuditingEntityListener.class)
public class Campaign {

    @Id
    @GeneratedValue(generator = &quot;uuid&quot;)
    @GenericGenerator(name = &quot;uuid&quot;, strategy = &quot;uuid2&quot;)
    private String id;

    @NotNull
    @Column(name = &quot;partner_name&quot;)
    private String partnerName;

    @NotNull
    @Column(name = &quot;partner_code&quot;)
    private String partnerCode;

    @NotNull
    @Column(name = &quot;campaign_name&quot;)
    private String campaignName;

    @NotNull
    @Column(name = &quot;campaign_code&quot;)
    private String campaignCode;

    // constructos, getters, setters, equals, hashcode

    @Override
    public String toString() {
        return &quot;Campaign{&quot; +
                &quot;id=&#39;&quot; + id + &#39;\&#39;&#39; +
                &quot;, partnerName=&#39;&quot; + partnerName + &#39;\&#39;&#39; +
                &quot;, partnerCode=&#39;&quot; + partnerCode + &#39;\&#39;&#39; +
                &quot;, campaignName=&#39;&quot; + campaignName + &#39;\&#39;&#39; +
                &quot;, campaignCode=&#39;&quot; + campaignCode + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }
    

However, when I call the findAll() method for the partners, this is my response:

为什么我的toString()方法不起作用,如何防止在SpringBootJPA中返回嵌套结果?

What I need is to not get the Campaigns atribute in the response when I get the findAll() partners method. I don't understand why my toString method is not working, if I have defined that I only what to print the id, partnerName and partnerCode.

So I don't want a nested response.

Thanks in advance.

答案1

得分: 3

你可以使用 @JsonIgnore 进行忽略,或者你可以在你的 @OneToMany 注解中添加一个属性以进行懒加载。将其替换为:

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
英文:

You can use @JsonIgnore to ignore it or you can add an attribute to your @OneToMany annotation to load it lazily. Replace it with:

@OneToMany(fetch=FetchType.Lazy, cascade = CascadeType.ALL, orphanRemoval = true)

huangapple
  • 本文由 发表于 2020年8月24日 00:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63549138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定