From 0e8b17698d65f60bf9a5065fec8284dd7397043e Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 18 Jul 2022 21:22:46 +0530 Subject: [PATCH] Fix imports and make database schema more strict. --- src/main/java/me/kavin/piped/utils/ResponseHelper.java | 4 ++-- src/main/java/me/kavin/piped/utils/obj/db/Channel.java | 2 +- .../java/me/kavin/piped/utils/obj/db/Playlist.java | 2 +- .../me/kavin/piped/utils/obj/db/PlaylistVideo.java | 4 ++-- src/main/java/me/kavin/piped/utils/obj/db/User.java | 10 ++++++---- src/main/java/me/kavin/piped/utils/obj/db/Video.java | 4 ++-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 6188b29..5b68539 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -967,7 +967,7 @@ public class ResponseHelper { if (user != null) { Multithreading.runAsync(() -> { - try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { + try (Session s = DatabaseSessionFactory.createSession()) { if (override) { user.setSubscribed(Set.of(channelIds)); } else { @@ -977,7 +977,7 @@ public class ResponseHelper { if (channelIds.length > 0) { var tr = s.beginTransaction(); - s.update(user); + s.merge(user); tr.commit(); } } catch (Exception e) { diff --git a/src/main/java/me/kavin/piped/utils/obj/db/Channel.java b/src/main/java/me/kavin/piped/utils/obj/db/Channel.java index 9fe986b..116995c 100644 --- a/src/main/java/me/kavin/piped/utils/obj/db/Channel.java +++ b/src/main/java/me/kavin/piped/utils/obj/db/Channel.java @@ -7,7 +7,7 @@ import jakarta.persistence.*; public class Channel { @Id - @Column(name = "uploader_id", length = 30) + @Column(name = "uploader_id", unique = true, nullable = false, length = 30) private String uploader_id; @Column(name = "uploader", length = 80) diff --git a/src/main/java/me/kavin/piped/utils/obj/db/Playlist.java b/src/main/java/me/kavin/piped/utils/obj/db/Playlist.java index c7b7c5a..97dfcae 100644 --- a/src/main/java/me/kavin/piped/utils/obj/db/Playlist.java +++ b/src/main/java/me/kavin/piped/utils/obj/db/Playlist.java @@ -40,7 +40,7 @@ public class Playlist { private String thumbnail; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "owner") + @JoinColumn(name = "owner", nullable = false) private User owner; @ManyToMany diff --git a/src/main/java/me/kavin/piped/utils/obj/db/PlaylistVideo.java b/src/main/java/me/kavin/piped/utils/obj/db/PlaylistVideo.java index 0a8c489..6dc673b 100644 --- a/src/main/java/me/kavin/piped/utils/obj/db/PlaylistVideo.java +++ b/src/main/java/me/kavin/piped/utils/obj/db/PlaylistVideo.java @@ -19,7 +19,7 @@ public class PlaylistVideo { } @Id - @Column(name = "id", unique = true, length = 16) + @Column(name = "id", unique = true, length = 16, nullable = false) private String id; @Column(name = "title", length = 120) @@ -32,7 +32,7 @@ public class PlaylistVideo { private String thumbnail; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "uploader_id") + @JoinColumn(name = "uploader_id", nullable = false) private Channel channel; public String getId() { diff --git a/src/main/java/me/kavin/piped/utils/obj/db/User.java b/src/main/java/me/kavin/piped/utils/obj/db/User.java index e3ab042..fe3ceb4 100644 --- a/src/main/java/me/kavin/piped/utils/obj/db/User.java +++ b/src/main/java/me/kavin/piped/utils/obj/db/User.java @@ -1,6 +1,7 @@ package me.kavin.piped.utils.obj.db; import jakarta.persistence.*; +import org.hibernate.annotations.Cascade; import java.io.Serializable; import java.util.Set; @@ -29,10 +30,11 @@ public class User implements Serializable { private String sessionId; @ElementCollection - @CollectionTable(name = "users_subscribed", joinColumns = @JoinColumn(name = "subscriber"), indexes = { - @Index(columnList = "subscriber", name = "users_subscribed_subscriber_idx"), - @Index(columnList = "channel", name = "users_subscribed_channel_idx")}) - @Column(name = "channel", length = 30) + @CollectionTable(name = "users_subscribed", joinColumns = @JoinColumn(name = "subscriber", nullable = false), + indexes = {@Index(columnList = "subscriber", name = "users_subscribed_subscriber_idx"), + @Index(columnList = "channel", name = "users_subscribed_channel_idx")}) + @Column(name = "channel", length = 30, nullable = false) + @Cascade(org.hibernate.annotations.CascadeType.ALL) private Set subscribed_ids; @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL) diff --git a/src/main/java/me/kavin/piped/utils/obj/db/Video.java b/src/main/java/me/kavin/piped/utils/obj/db/Video.java index 6d12486..a6a648d 100644 --- a/src/main/java/me/kavin/piped/utils/obj/db/Video.java +++ b/src/main/java/me/kavin/piped/utils/obj/db/Video.java @@ -9,7 +9,7 @@ import jakarta.persistence.*; public class Video { @Id - @Column(name = "id", unique = true, length = 16) + @Column(name = "id", unique = true, length = 16, nullable = false) private String id; @Column(name = "title", length = 120) @@ -28,7 +28,7 @@ public class Video { private String thumbnail; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "uploader_id") + @JoinColumn(name = "uploader_id", nullable = false) private Channel channel; public Video() {