0

I've been trying to make an API using spring boot. I have two entities, Donor and Donation. What I want to do is add a Donor first using an endpoint and assign Donations to it. I also want to do it vice versa where I add a Donation and add a Donor to it.

Right now I can only do the former where I add a Donor and assign a Donation. I cannot seem to implement the latter even though I followed the same implementation of saving the entity in my Donor Service class.

Donor Entity

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table
public class Donor extends Auditable{
    @Id
    @GeneratedValue
    private Long id;
    private Long donationId;
    private String donorName;
    private String accountNumber;
    private String accountName;
    private String companyTIN;
    private String companyAddress;
    private String address1;
    private String address2;
    private String address3;
    private String address4;
    private String address5;
    private String phone1;
    private String phone2;
    private String faxNumber;
    private String cellphoneNumber;
    private String emailAddress;
    private String salutation;
    private String birthDate;
    private String notes;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "donor_donation",
            joinColumns = @JoinColumn(name = "donor_id"),
            inverseJoinColumns = @JoinColumn(name = "donation_id")
    )
    private List<Donation> donations = new ArrayList<>();

    public void addDonation(Donation donation) {
        donations.add(donation);
    }
}

Donation Entity:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table
public class Donation extends Auditable {
    @Id
    @GeneratedValue
    private Long id;
    private Long donorId;
    private Long scholarshipId;
    private String accountNumber;
    private String accountName;
    private String orNumber;
    private String date;
    private Double amount;
    private String notes;
//    private ArrayList<String> orFiles;
//    private ArrayList<String> tyFiles;
//    private ArrayList<String> codFiles;
    private String needCertificate;
    private String purposeOfDonation;

    @ManyToMany(mappedBy = "donations")
    @JsonIgnore
    private List<Donor> donors = new ArrayList<>();

    public void addDonor(Donor donor) {
        donors.add(donor);
    }
}

Snippet of save donor code from Donor Service (Does not save donor to donation): The code runs the if statement but the Donor does not get saved to the donation.

public Donor saveDonor(Donor donor) {
        if (donor.getDonationId() != null) {
            Donation donation = donationRepository.findById(donor.getDonationId()).orElse(null);
            donation.addDonor(donor);

            return donorRepository.save(donor);
        }
        return donorRepository.save(donor);
    }

Snippet of save donation to donor code from Donation Service (works properly):

    public Donation saveDonation(Donation donation) {
        Donor donor = donorRepository.findById(donation.getDonorId()).orElse(null);

        donor.addDonation(donation);
        return donationRepository.save(donation);
    }

Sample Controller Snippet:

    @PostMapping("/add")
    public Donation addDonation(@RequestBody Donation donation) {
        return donationService.saveDonation(donation);
    }
Soundwave98
  • 73
  • 1
  • 7
  • Does this answer your question? [What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association?](https://stackoverflow.com/questions/2584521/what-is-the-inverse-side-of-the-association-in-a-bidirectional-jpa-onetomany-m) – crizzis Mar 21 '21 at 16:40
  • Not exactly. I was pertaining to the case where (in the post’s example) an order can have many customers too (say a group of friends pitched in for that order). In that case, both customer and order can be orders. – Soundwave98 Mar 22 '21 at 00:17
  • That doesn't make any difference. The bottom line is: changes to the inverse side of the association are ignored by JPA. You could pretend both sides are the owning side (by removing `mappedBy` and adding the appropriate `@JoinTable` annotation on top of `Donation.donors`), but it's safer to control the association using one side only – crizzis Mar 22 '21 at 09:37

0 Answers0