From 9f5a62bb82aaa1bcc879d55b3dcf53d0aabb5ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Vrba?= Date: Fri, 4 Oct 2024 19:04:19 +0200 Subject: [PATCH] Replace JPA with JDBC because fuck JPA --- api/build.gradle.kts | 2 +- .../java/dev/vrba/dubs/domain/Channel.java | 17 ++++---- .../main/java/dev/vrba/dubs/domain/Guild.java | 24 +++++------ .../main/java/dev/vrba/dubs/domain/Match.java | 40 ++++++++----------- .../dev/vrba/dubs/domain/MatchedPattern.java | 25 +++++++----- .../main/java/dev/vrba/dubs/domain/User.java | 22 +++++----- .../dubs/repository/ChannelRepository.java | 5 ++- .../vrba/dubs/repository/GuildRepository.java | 5 ++- .../vrba/dubs/repository/MatchRepository.java | 5 ++- .../repository/MatchedPatternRepository.java | 5 ++- .../vrba/dubs/repository/UserRepository.java | 5 ++- .../dev/vrba/dubs/service/GuildService.java | 3 +- api/src/main/resources/application.yml | 2 + 13 files changed, 77 insertions(+), 83 deletions(-) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 8446808..a6ec3fb 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -19,7 +19,7 @@ dependencies { annotationProcessor("io.micronaut.security:micronaut-security-annotations") annotationProcessor("io.micronaut.serde:micronaut-serde-processor") implementation("io.micronaut:micronaut-management") - implementation("io.micronaut.data:micronaut-data-hibernate-jpa") + implementation("io.micronaut.data:micronaut-data-jdbc") implementation("io.micronaut.flyway:micronaut-flyway") implementation("io.micronaut.micrometer:micronaut-micrometer-core") implementation("io.micronaut.micrometer:micronaut-micrometer-registry-prometheus") diff --git a/api/src/main/java/dev/vrba/dubs/domain/Channel.java b/api/src/main/java/dev/vrba/dubs/domain/Channel.java index dcb5b88..26a5641 100644 --- a/api/src/main/java/dev/vrba/dubs/domain/Channel.java +++ b/api/src/main/java/dev/vrba/dubs/domain/Channel.java @@ -1,29 +1,28 @@ package dev.vrba.dubs.domain; import io.micronaut.core.annotation.NonNull; +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; +import io.micronaut.data.annotation.MappedProperty; import io.micronaut.data.annotation.sql.JoinColumn; -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.With; +import lombok.*; @With @Getter -@Entity(name = "channels") +@Setter @NoArgsConstructor @AllArgsConstructor +@MappedEntity("channels") public class Channel { @Id @NonNull - @Column(name = "id", nullable = false) + @MappedProperty("id") private String id; @NonNull - @Column(name = "name", nullable = false) + @MappedProperty("name") private String name; @JoinColumn(name = "guild_id", referencedColumnName = "id") - @ManyToOne(fetch = FetchType.EAGER, optional = false) private Guild guild; } diff --git a/api/src/main/java/dev/vrba/dubs/domain/Guild.java b/api/src/main/java/dev/vrba/dubs/domain/Guild.java index 890e566..ce7d554 100644 --- a/api/src/main/java/dev/vrba/dubs/domain/Guild.java +++ b/api/src/main/java/dev/vrba/dubs/domain/Guild.java @@ -1,34 +1,28 @@ package dev.vrba.dubs.domain; import io.micronaut.core.annotation.NonNull; -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.With; - -import java.util.Set; +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; +import io.micronaut.data.annotation.MappedProperty; +import lombok.*; @With @Getter -@Entity(name = "guilds") +@Setter @NoArgsConstructor @AllArgsConstructor +@MappedEntity("guilds") public class Guild { @Id @NonNull - @Column(name = "id", nullable = false, updatable = false) + @MappedProperty("id") private String id; @NonNull - @Column(name = "name", nullable = false) + @MappedProperty("name") private String name; @NonNull - @Column(name = "icon_url", nullable = false) + @MappedProperty("icon_url") private String icon; - - @NonNull - @OneToMany(fetch = FetchType.LAZY, mappedBy = "guild") - private Set channels; } diff --git a/api/src/main/java/dev/vrba/dubs/domain/Match.java b/api/src/main/java/dev/vrba/dubs/domain/Match.java index 6fd94ea..157622d 100644 --- a/api/src/main/java/dev/vrba/dubs/domain/Match.java +++ b/api/src/main/java/dev/vrba/dubs/domain/Match.java @@ -1,58 +1,52 @@ package dev.vrba.dubs.domain; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.validation.constraints.Size; +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; +import io.micronaut.data.annotation.MappedProperty; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import java.math.BigInteger; @Getter -@Entity(name = "matches") +@Setter @NoArgsConstructor @AllArgsConstructor +@MappedEntity("matches") public class Match { @Id - @Column(name = "row_id") + @MappedProperty("row_id") private Integer id; - @Size(max = 64) - @Column(name = "pattern_name", length = 64) + @MappedProperty("pattern_name") private String patternName; - @Column(name = "pattern_points") + @MappedProperty("pattern_points") private Long patternPoints; - @Column(name = "pattern_is_rare") + @MappedProperty("pattern_is_rare") private Boolean patternIsRare; - @Size(max = 32) - @Column(name = "user_id", length = 32) + @MappedProperty("user_id") private String userId; - @Size(max = 128) - @Column(name = "user_name", length = 128) + @MappedProperty("user_name") private String userName; - @Size(max = 32) - @Column(name = "channel_id", length = 32) + @MappedProperty("channel_id") private String channelId; - @Size(max = 128) - @Column(name = "channel_name", length = 128) + @MappedProperty("channel_name") private String channelName; - @Size(max = 32) - @Column(name = "guild_id", length = 32) + @MappedProperty("guild_id") private String guildId; - @Size(max = 128) - @Column(name = "guild_name", length = 128) + @MappedProperty("guild_name") private String guildName; - @Column(name = "count") + @MappedProperty("count") private BigInteger count; } diff --git a/api/src/main/java/dev/vrba/dubs/domain/MatchedPattern.java b/api/src/main/java/dev/vrba/dubs/domain/MatchedPattern.java index 89e0746..3197695 100644 --- a/api/src/main/java/dev/vrba/dubs/domain/MatchedPattern.java +++ b/api/src/main/java/dev/vrba/dubs/domain/MatchedPattern.java @@ -1,41 +1,44 @@ package dev.vrba.dubs.domain; import io.micronaut.core.annotation.NonNull; -import jakarta.persistence.*; +import io.micronaut.data.annotation.GeneratedValue; +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; +import io.micronaut.data.annotation.MappedProperty; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.RequiredArgsConstructor; -import lombok.experimental.FieldNameConstants; +import lombok.Setter; @Getter -@Entity(name = "matched_patterns") +@Setter @NoArgsConstructor @AllArgsConstructor +@MappedEntity("matched_patterns") public class MatchedPattern { @Id @NonNull - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", nullable = false, updatable = false, insertable = false) + @GeneratedValue() + @MappedProperty("id") private Long id = null; @NonNull - @Column(name = "channel_id", nullable = false) + @MappedProperty("channel_id") private String channel; @NonNull - @Column(name = "user_id", nullable = false) + @MappedProperty("user_id") private String user; @NonNull - @Column(name = "pattern_name", nullable = false) + @MappedProperty("pattern_name") private String patternName; @NonNull - @Column(name = "pattern_points", nullable = false) + @MappedProperty("pattern_points") private long patternPoints; @NonNull - @Column(name = "pattern_is_rare", nullable = false) + @MappedProperty("pattern_is_rare") private boolean patternIsRare; } diff --git a/api/src/main/java/dev/vrba/dubs/domain/User.java b/api/src/main/java/dev/vrba/dubs/domain/User.java index 88c6d53..550cf44 100644 --- a/api/src/main/java/dev/vrba/dubs/domain/User.java +++ b/api/src/main/java/dev/vrba/dubs/domain/User.java @@ -1,36 +1,34 @@ package dev.vrba.dubs.domain; import io.micronaut.core.annotation.NonNull; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.With; +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; +import io.micronaut.data.annotation.MappedProperty; +import lombok.*; import java.math.BigInteger; @With @Getter -@Entity(name = "users") +@Setter +@MappedEntity("users") @NoArgsConstructor @AllArgsConstructor public class User { @Id @NonNull - @Column(name = "id", nullable = false, updatable = false) + @MappedProperty("id") private String id; @NonNull - @Column(name = "name", nullable = false) + @MappedProperty("name") private String name; @NonNull - @Column(name = "avatar_url", nullable = false) + @MappedProperty("avatar_url") private String avatar; @NonNull - @Column(name = "points", nullable = false) + @MappedProperty("points") private BigInteger points; } diff --git a/api/src/main/java/dev/vrba/dubs/repository/ChannelRepository.java b/api/src/main/java/dev/vrba/dubs/repository/ChannelRepository.java index 4a1ef2d..e7c08f8 100644 --- a/api/src/main/java/dev/vrba/dubs/repository/ChannelRepository.java +++ b/api/src/main/java/dev/vrba/dubs/repository/ChannelRepository.java @@ -1,9 +1,10 @@ package dev.vrba.dubs.repository; import dev.vrba.dubs.domain.Channel; -import io.micronaut.data.annotation.Repository; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; import io.micronaut.data.repository.CrudRepository; -@Repository +@JdbcRepository(dialect = Dialect.POSTGRES) public interface ChannelRepository extends CrudRepository { } diff --git a/api/src/main/java/dev/vrba/dubs/repository/GuildRepository.java b/api/src/main/java/dev/vrba/dubs/repository/GuildRepository.java index b59465f..272a2bc 100644 --- a/api/src/main/java/dev/vrba/dubs/repository/GuildRepository.java +++ b/api/src/main/java/dev/vrba/dubs/repository/GuildRepository.java @@ -1,9 +1,10 @@ package dev.vrba.dubs.repository; import dev.vrba.dubs.domain.Guild; -import io.micronaut.data.annotation.Repository; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; import io.micronaut.data.repository.CrudRepository; -@Repository +@JdbcRepository(dialect = Dialect.POSTGRES) public interface GuildRepository extends CrudRepository { } diff --git a/api/src/main/java/dev/vrba/dubs/repository/MatchRepository.java b/api/src/main/java/dev/vrba/dubs/repository/MatchRepository.java index 6db4ed8..879d87c 100644 --- a/api/src/main/java/dev/vrba/dubs/repository/MatchRepository.java +++ b/api/src/main/java/dev/vrba/dubs/repository/MatchRepository.java @@ -1,9 +1,10 @@ package dev.vrba.dubs.repository; import dev.vrba.dubs.domain.Match; -import io.micronaut.data.annotation.Repository; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; import io.micronaut.data.repository.CrudRepository; -@Repository +@JdbcRepository(dialect = Dialect.POSTGRES) public interface MatchRepository extends CrudRepository { } diff --git a/api/src/main/java/dev/vrba/dubs/repository/MatchedPatternRepository.java b/api/src/main/java/dev/vrba/dubs/repository/MatchedPatternRepository.java index e31e85c..d32b183 100644 --- a/api/src/main/java/dev/vrba/dubs/repository/MatchedPatternRepository.java +++ b/api/src/main/java/dev/vrba/dubs/repository/MatchedPatternRepository.java @@ -1,9 +1,10 @@ package dev.vrba.dubs.repository; import dev.vrba.dubs.domain.MatchedPattern; -import io.micronaut.data.annotation.Repository; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; import io.micronaut.data.repository.CrudRepository; -@Repository +@JdbcRepository(dialect = Dialect.POSTGRES) public interface MatchedPatternRepository extends CrudRepository { } diff --git a/api/src/main/java/dev/vrba/dubs/repository/UserRepository.java b/api/src/main/java/dev/vrba/dubs/repository/UserRepository.java index 1f93f97..55e9f96 100644 --- a/api/src/main/java/dev/vrba/dubs/repository/UserRepository.java +++ b/api/src/main/java/dev/vrba/dubs/repository/UserRepository.java @@ -1,9 +1,10 @@ package dev.vrba.dubs.repository; import dev.vrba.dubs.domain.User; -import io.micronaut.data.annotation.Repository; +import io.micronaut.data.jdbc.annotation.JdbcRepository; +import io.micronaut.data.model.query.builder.sql.Dialect; import io.micronaut.data.repository.CrudRepository; -@Repository +@JdbcRepository(dialect = Dialect.POSTGRES) public interface UserRepository extends CrudRepository { } diff --git a/api/src/main/java/dev/vrba/dubs/service/GuildService.java b/api/src/main/java/dev/vrba/dubs/service/GuildService.java index f85abee..69742c7 100644 --- a/api/src/main/java/dev/vrba/dubs/service/GuildService.java +++ b/api/src/main/java/dev/vrba/dubs/service/GuildService.java @@ -6,7 +6,6 @@ import jakarta.inject.Singleton; import lombok.RequiredArgsConstructor; -import java.util.HashSet; import java.util.Optional; @Singleton @@ -25,6 +24,6 @@ public Guild upsertGuild( final Optional existingGuild = repository.findById(id); return existingGuild .map(entity -> repository.update(entity.withName(name).withIcon(icon))) - .orElseGet(() -> repository.save(new Guild(id, name, icon, new HashSet<>()))); + .orElseGet(() -> repository.save(new Guild(id, name, icon))); } } diff --git a/api/src/main/resources/application.yml b/api/src/main/resources/application.yml index dee8bcf..8f7464e 100644 --- a/api/src/main/resources/application.yml +++ b/api/src/main/resources/application.yml @@ -64,6 +64,8 @@ flyway: jpa: default: + entity-scan: + packages: dev.vrba.dubs.domain properties: hibernate: hbm2ddl: