From 3ca73361f059841c8b63adb138ce7265b0fb3847 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 11 May 2020 20:21:05 +0200 Subject: [PATCH] Introduce proper many to one relation --- .../hft/geotime/timetrackaccount/TimetrackAccount.java | 8 ++++---- .../main/java/de/hft/geotime/user/TimetrackUser.java | 2 +- backend/src/main/resources/data.sql | 10 +++------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java b/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java index 9f98630..5f95ff4 100644 --- a/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java +++ b/backend/src/main/java/de/hft/geotime/timetrackaccount/TimetrackAccount.java @@ -1,13 +1,11 @@ package de.hft.geotime.timetrackaccount; +import de.hft.geotime.user.TimetrackUser; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; @Data @NoArgsConstructor @@ -21,5 +19,7 @@ public class TimetrackAccount { private double revenue; private String name; private String description; + @ManyToOne + private TimetrackUser user; } diff --git a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java index 66c2cea..ff3c30c 100644 --- a/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java +++ b/backend/src/main/java/de/hft/geotime/user/TimetrackUser.java @@ -33,7 +33,7 @@ public class TimetrackUser { @OneToOne private Role role; - @OneToMany(fetch = FetchType.LAZY) + @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL) private List accounts; } diff --git a/backend/src/main/resources/data.sql b/backend/src/main/resources/data.sql index 7e008c2..1668db4 100644 --- a/backend/src/main/resources/data.sql +++ b/backend/src/main/resources/data.sql @@ -6,10 +6,6 @@ DELETE FROM role; INSERT INTO role (id, `name`) VALUES (1, 'Admin'); -INSERT INTO timetrack_account (id, description, `name`, revenue) VALUES - (1, 'Gleitzeit Marcel', 'Primary Marcel', 16.0), - (2, 'Festgeld Marcel', 'Secondary Marcel', 25.0); - /* password is the firstname in lowercase e.g. marcel or tobias https://bcrypt-generator.com/ with 10 rounds */ @@ -19,8 +15,8 @@ INSERT INTO timetrack_user (id, firstname, lastname, password, username, role_id (3, 'Tim', 'Zieger' ,'$2y$10$pYGHZhoaelceImO7aIN4nOkWJBp.oqNGFYaRAonHkYF4u9ljqPelC', 'ziti', 1), (4, 'Simon', 'Kellner' ,'$2y$10$Puzm/Nr/Dyq3nQxlkXGIfubS5JPtXJSOf2e6mrQ6HhVYQN9YiQQsC', 'kesi', 1); -INSERT INTO timetrack_user_accounts (timetrack_user_id, accounts_id) VALUES - (1, 1), - (1, 2); +INSERT INTO timetrack_account (description, `name`, revenue, user_id) VALUES + ('Gleitzeit Marcel', 'Primary Marcel', 16.0, 1), + ('Festgeld Marcel', 'Secondary Marcel', 25.0, 1); SET FOREIGN_KEY_CHECKS=1; \ No newline at end of file