From f9ee2a39e0bf595796044a32c50c7ede00b2170d Mon Sep 17 00:00:00 2001 From: Michael Harlow <94586121+michaelharlow@users.noreply.github.com> Date: Tue, 5 Nov 2024 23:17:28 -0600 Subject: [PATCH] db: add one to many relationship --- .../com/avengers/example/domain/Account.java | 22 +++++++++++-------- .../com/avengers/example/domain/Loan.java | 12 ++++++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/avengers/example/domain/Account.java b/src/main/java/com/avengers/example/domain/Account.java index 771f865..de075d2 100644 --- a/src/main/java/com/avengers/example/domain/Account.java +++ b/src/main/java/com/avengers/example/domain/Account.java @@ -1,17 +1,18 @@ package com.avengers.example.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; +import jakarta.persistence.*; import lombok.Data; import lombok.Getter; -import lombok.Setter; +import java.util.ArrayList; +import java.util.List; @Entity @Data @Getter +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class Account { @Id @@ -23,6 +24,9 @@ public class Account private String email; private String phoneNumber; + @OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true) + private List loans = new ArrayList<>(); + /** * No argument constructor for account object. */ @@ -51,7 +55,7 @@ public void setIsAdmin(boolean isAdmin) public void setUsername(String username) { - if (null == username || username == "" || username == " ") + if (null == username || username.isEmpty() || username.equals(" ")) { throw new IllegalArgumentException("Username can not be null or empty!"); } @@ -65,7 +69,7 @@ public void setUsername(String username) */ public void setPassword(String password) { - if (null == password || password == "" || password == " ") + if (null == password || password.isEmpty() || password.equals(" ")) { throw new IllegalArgumentException("Password can not be null or empty!"); } @@ -79,7 +83,7 @@ public void setPassword(String password) */ public void setEmail(String email) { - if (null == email || email == "" || email == " ") + if (null == email || email.isEmpty() || email.equals(" ")) { throw new IllegalArgumentException("Email can not be null or empty!"); } @@ -93,7 +97,7 @@ public void setEmail(String email) */ public void setPhoneNumber(String phoneNumber) { - if (null == phoneNumber || phoneNumber == "" || phoneNumber == " ") + if (null == phoneNumber || phoneNumber.isEmpty() || phoneNumber.equals(" ")) { throw new IllegalArgumentException("Phone Number can not be null or empty!"); } diff --git a/src/main/java/com/avengers/example/domain/Loan.java b/src/main/java/com/avengers/example/domain/Loan.java index b3d5e8e..c269555 100644 --- a/src/main/java/com/avengers/example/domain/Loan.java +++ b/src/main/java/com/avengers/example/domain/Loan.java @@ -1,9 +1,8 @@ package com.avengers.example.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; +import jakarta.persistence.*; import lombok.Data; import lombok.Getter; import lombok.Setter; @@ -12,6 +11,7 @@ @Entity @Getter @Setter +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class Loan { @Id @@ -20,6 +20,10 @@ public class Loan private double originAmount; private long interestRate; + @ManyToOne + @JoinColumn(name = "account_id") + private Account account; + public Loan() { }