Skip to content

Commit

Permalink
db: add one to many relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelharlow committed Nov 6, 2024
1 parent aeeb22f commit f9ee2a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/main/java/com/avengers/example/domain/Account.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,6 +24,9 @@ public class Account
private String email;
private String phoneNumber;

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Loan> loans = new ArrayList<>();

/**
* No argument constructor for account object.
*/
Expand Down Expand Up @@ -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!");
}
Expand All @@ -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!");
}
Expand All @@ -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!");
}
Expand All @@ -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!");
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/avengers/example/domain/Loan.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +11,7 @@
@Entity
@Getter
@Setter
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Loan
{
@Id
Expand All @@ -20,6 +20,10 @@ public class Loan
private double originAmount;
private long interestRate;

@ManyToOne
@JoinColumn(name = "account_id")
private Account account;

public Loan()
{
}
Expand Down

0 comments on commit f9ee2a3

Please sign in to comment.