英文:
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() 方法获取合作伙伴时,这是我的响应:
我需要的是在获取 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 = "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<>();
// constructos, getters, setters, equals, hashcode
@Override
public String toString() {
return "Partner{" +
"id='" + id + '\'' +
", partnerName='" + partnerName + '\'' +
", partnerCode='" + partnerCode + '\'' +
'}';
}
This is my Campaign entity:
@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;
// constructos, getters, setters, equals, hashcode
@Override
public String toString() {
return "Campaign{" +
"id='" + id + '\'' +
", partnerName='" + partnerName + '\'' +
", partnerCode='" + partnerCode + '\'' +
", campaignName='" + campaignName + '\'' +
", campaignCode='" + campaignCode + '\'' +
'}';
}
However, when I call the findAll() method for the partners, this is my response:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论