Skip to content

Commit

Permalink
Replace JPA with JDBC because fuck JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkavrba committed Oct 4, 2024
1 parent 3207b72 commit 9f5a62b
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 83 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 8 additions & 9 deletions api/src/main/java/dev/vrba/dubs/domain/Channel.java
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 9 additions & 15 deletions api/src/main/java/dev/vrba/dubs/domain/Guild.java
Original file line number Diff line number Diff line change
@@ -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<Channel> channels;
}
40 changes: 17 additions & 23 deletions api/src/main/java/dev/vrba/dubs/domain/Match.java
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 14 additions & 11 deletions api/src/main/java/dev/vrba/dubs/domain/MatchedPattern.java
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 10 additions & 12 deletions api/src/main/java/dev/vrba/dubs/domain/User.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Channel, String> {
}
Original file line number Diff line number Diff line change
@@ -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<Guild, String> {
}
Original file line number Diff line number Diff line change
@@ -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<Match, Integer> {
}
Original file line number Diff line number Diff line change
@@ -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<MatchedPattern, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<User, String> {
}
3 changes: 1 addition & 2 deletions api/src/main/java/dev/vrba/dubs/service/GuildService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import jakarta.inject.Singleton;
import lombok.RequiredArgsConstructor;

import java.util.HashSet;
import java.util.Optional;

@Singleton
Expand All @@ -25,6 +24,6 @@ public Guild upsertGuild(
final Optional<Guild> 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)));
}
}
2 changes: 2 additions & 0 deletions api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ flyway:

jpa:
default:
entity-scan:
packages: dev.vrba.dubs.domain
properties:
hibernate:
hbm2ddl:
Expand Down

0 comments on commit 9f5a62b

Please sign in to comment.