From a9fd3343f2b5ae6d80fbc0408b7b71f4ebdffc6e Mon Sep 17 00:00:00 2001 From: Hsbalazs Date: Fri, 30 Aug 2024 20:42:43 +0200 Subject: [PATCH] feat: create join connection between _user and _pet table --- .../java/com/greenfoxacademy/backend/models/Pet.java | 9 ++++++++- .../java/com/greenfoxacademy/backend/models/User.java | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/com/greenfoxacademy/backend/models/Pet.java b/backend/src/main/java/com/greenfoxacademy/backend/models/Pet.java index e350c560..b213cd56 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/models/Pet.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/models/Pet.java @@ -1,9 +1,12 @@ package com.greenfoxacademy.backend.models; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.util.Date; import lombok.AllArgsConstructor; @@ -34,4 +37,8 @@ public class Pet { private Date petBirthDate; private Date lastCheckUp; private Date nextCheckUp; -} \ No newline at end of file + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "petOwner_Id") + private User petOwner; +} diff --git a/backend/src/main/java/com/greenfoxacademy/backend/models/User.java b/backend/src/main/java/com/greenfoxacademy/backend/models/User.java index 2d40c708..c2a972ac 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/models/User.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/models/User.java @@ -1,10 +1,14 @@ package com.greenfoxacademy.backend.models; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; @@ -38,6 +42,9 @@ public class User implements UserDetails { private String password; private UUID verificationId; + @OneToMany(mappedBy = "petOwner", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private List pets = new ArrayList<>(); + @Override public Collection getAuthorities() { return List.of(new SimpleGrantedAuthority("ROLE_USER"));