-2

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:

enter image description here

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.

Caroline
  • 373
  • 1
  • 5
  • 13
  • 3
    findAll will return you entities with all the fields, it doesn't use toString() while creating db records to java object – Deepak Patankar Aug 23 '20 at 16:08
  • 2
    Look for JsonIgnore annotation. toString isn't called in that findAlll operation. – SKumar Aug 23 '20 at 16:08
  • 3
    You can use this answer for reference https://stackoverflow.com/questions/37167422/how-to-fetch-only-selected-attributes-of-an-entity-using-spring-jpa. Here the user is only returning some specific fields – Deepak Patankar Aug 23 '20 at 16:13
  • This answer also could help with lazy loading config: https://stackoverflow.com/a/47713752/3010548 – Damião Martins Aug 23 '20 at 16:25

1 Answers1

3

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)
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
DK93
  • 89
  • 1
  • 14