Reputation: 83
I'm making a DBMS application where I would add a Donor, Donation, and MOA (Memorandum of Agreement) row entry. What I want to accomplish with this application is when I add those three rows using inputs from my frontend and press a button those three would automatically link up together.
Here is the ERD for the relationship. It's a classic many to many with a bridge type of relationship.
The Code
Donor.java
public class Donor extends Auditable implements Comparable<Donor>{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Cannot have an empty account number field.")
private String accountNumber;
private String accountName;
private String donorName;
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 LocalDate birthDate;
private String notes;
@OneToMany(mappedBy = "donor")
List<MOA> moaList = new ArrayList<>();
...
MOA.java
public class Donor extends Auditable implements Comparable<Donor>{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Cannot have an empty account number field.")
private String accountNumber;
private String accountName;
private String donorName;
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 LocalDate birthDate;
private String notes;
@OneToMany(mappedBy = "donor")
List<MOA> moaList = new ArrayList<>();
...
Donation.java
public class Donation extends Auditable implements Comparable<Donation> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Cannot have an empty account number field.")
private String accountNumber;
private String accountName;
private String orNumber;
private String date;
private Double amount;
private String notes;
private String needCertificate;
private String purposeOfDonation;
@OneToMany(mappedBy = "donation")
List<MOA> moaList = new ArrayList<>();
...
What should I write on one of my services to have this feature of being able to sync the tables together?
Upvotes: 0
Views: 251
Reputation: 89
public class MOA {
...
@ManyToOne
private Donor donor;
@ManyToOne
private Donation donation;
//getters and setters
public addDonor(Donor donor){
this.setDonor(donor);
}
public addDonation(Donation donation){
this.setDonation(donation);
}
}
For linking up all the three entities
MOA moa = new MOA();
Donor donor = new Donor();
Donation donation =new Donation();
//enter the values for fields in donor, donation,moa class by using setter methods
moa.setDonor(donor);
moa.setDonation(donation);//by setting these values all three entities will be saved.
Upvotes: 0
Reputation: 591
Try this:
public class Donor extends Auditable implements Comparable<Donor>{
...
@OneToMany(mappedBy = "donor")
List<MOA> moaList = new ArrayList<>();
public class MOA {
...
@ManyToOne
private Donor donot;
@ManyToOne
private Donation donation;
...
public class Donation extends Auditable implements Comparable<Donation> {
...
@OneToMany(mappedBy = "donation")
List<MOA> moaList = new ArrayList<>();
...
Upvotes: 1