Pārlūkot izejas kodu

added relationship between user and game

Rachelle 6 gadus atpakaļ
vecāks
revīzija
a3f408e95c

+ 2
- 1
MyPassionProject/src/main/java/com/example/demo/Entity/Game.java Parādīt failu

@@ -3,12 +3,13 @@ package com.example.demo.Entity;
3 3
 
4 4
 import javax.persistence.Entity;
5 5
 import javax.persistence.GeneratedValue;
6
+import javax.persistence.GenerationType;
6 7
 import javax.persistence.Id;
7 8
 
8 9
 @Entity
9 10
 public class Game {
10 11
   @Id
11
-  @GeneratedValue
12
+  @GeneratedValue(strategy = GenerationType.IDENTITY)
12 13
   private long id;
13 14
   private String name;
14 15
   private String description;

+ 37
- 4
MyPassionProject/src/main/java/com/example/demo/Entity/Library.java Parādīt failu

@@ -1,4 +1,37 @@
1
-package com.example.demo.Entity;
2
-
3
-public class Library {
4
-}
1
+//package com.example.demo.Entity;
2
+//
3
+//import javax.persistence.*;
4
+//import java.util.HashSet;
5
+//import java.util.Set;
6
+//
7
+//@Entity
8
+//public class Library {
9
+//    @Id
10
+//    @GeneratedValue
11
+//    Long libraryId;
12
+//
13
+//    @ManyToMany(fetch = FetchType.LAZY,
14
+//            cascade = {
15
+//                    CascadeType.PERSIST,
16
+//                    CascadeType.MERGE
17
+//            })
18
+//    @JoinTable(name = "game_library",
19
+//        joinColumns = {@JoinColumn(name = "id")},
20
+//    inverseJoinColumns = {@JoinColumn(name = "game_id")})
21
+//    private Set<Game> games;
22
+//
23
+//    public Set<Game> getGames() {
24
+//        return games;
25
+//    }
26
+//
27
+//    public void setGames(Set<Game> games) {
28
+//        this.games = games;
29
+//    }
30
+//
31
+//    public void addGames(Game game){
32
+//        if(games==null){
33
+//            this.games = new HashSet<Game>();
34
+//        }
35
+//        games.add(game);
36
+//    }
37
+//}

+ 14
- 10
MyPassionProject/src/main/java/com/example/demo/Entity/User.java Parādīt failu

@@ -8,7 +8,7 @@ import java.util.Set;
8 8
 public class User {
9 9
 
10 10
     @Id
11
-    @GeneratedValue
11
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
12 12
     private long id;
13 13
     private String summary;
14 14
     private String profileName;
@@ -20,9 +20,9 @@ public class User {
20 20
                     CascadeType.MERGE
21 21
             })
22 22
     @JoinTable(name = "user_game",
23
-        joinColumns = {@JoinColumn(name = "user_id")},
24
-    inverseJoinColumns = {@JoinColumn(name = "game_id")})
25
-    private Set<Game> games;
23
+            joinColumns = {@JoinColumn(name = "user_id")},
24
+            inverseJoinColumns = {@JoinColumn(name = "game_id")})
25
+    private Set<Game> gameLibrary;
26 26
 
27 27
     public long getId() {
28 28
         return id;
@@ -52,16 +52,20 @@ public class User {
52 52
         this.userName = userName;
53 53
     }
54 54
 
55
-    public Set<Game> getGames() { return games; }
56 55
 
57
-    public void setGames(Set<Game> gameIds) {
58
-        this.games = games;
56
+    public Set<Game> getGameLibrary() {
57
+        return gameLibrary;
58
+    }
59
+
60
+    public void setGameLibrary(Set<Game> gameLibrary) {
61
+        this.gameLibrary = gameLibrary;
59 62
     }
60 63
 
61 64
     public void addGames(Game game){
62
-        if(games==null){
63
-            this.games = new HashSet<Game>();
65
+        if(gameLibrary==null){
66
+            this.gameLibrary = new HashSet<Game>();
64 67
         }
65
-        games.add(game);
68
+        gameLibrary.add(game);
66 69
     }
70
+
67 71
 }