From 9aedf1200bb3d0e1dab21c819fced7961e7770c2 Mon Sep 17 00:00:00 2001 From: Kevin Birk Date: Mon, 19 Aug 2024 18:25:57 -0400 Subject: [PATCH] Add auto increment field and sort on that. (#4497) Co-authored-by: kbirk --- packages/client/hmi-client/src/types/Types.ts | 1 + .../dataservice/simulation/Simulation.java | 17 ++++++++++------- .../simulation/SimulationUpdate.java | 17 ++++++++++++++--- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/client/hmi-client/src/types/Types.ts b/packages/client/hmi-client/src/types/Types.ts index c5f9574432..11d030e700 100644 --- a/packages/client/hmi-client/src/types/Types.ts +++ b/packages/client/hmi-client/src/types/Types.ts @@ -518,6 +518,7 @@ export interface Simulation extends TerariumAsset { } export interface SimulationUpdate extends TerariumEntity { + sequenceNumber: number; data: any; simulation: Simulation; } diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/Simulation.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/Simulation.java index 268f3fbcb7..1ec4471df3 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/Simulation.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/Simulation.java @@ -1,9 +1,18 @@ package software.uncharted.terarium.hmiserver.models.dataservice.simulation; +import java.io.Serial; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.hibernate.annotations.Type; + import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.databind.JsonNode; + import io.hypersistence.utils.hibernate.type.json.JsonType; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.persistence.CascadeType; @@ -14,15 +23,9 @@ import jakarta.persistence.FetchType; import jakarta.persistence.OneToMany; import jakarta.persistence.OrderBy; -import java.io.Serial; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; -import org.hibernate.annotations.Type; import software.uncharted.terarium.hmiserver.annotations.TSModel; import software.uncharted.terarium.hmiserver.annotations.TSOptional; import software.uncharted.terarium.hmiserver.models.TerariumAsset; @@ -88,7 +91,7 @@ public class Simulation extends TerariumAsset { private UUID projectId; @OneToMany(mappedBy = "simulation", fetch = FetchType.EAGER, cascade = CascadeType.ALL) - @OrderBy("createdOn DESC") + @OrderBy("sequenceNumber DESC") @JsonManagedReference private List updates = new ArrayList<>(); diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/SimulationUpdate.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/SimulationUpdate.java index 9a130425dc..dda77cdd3f 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/SimulationUpdate.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/dataservice/simulation/SimulationUpdate.java @@ -1,17 +1,23 @@ package software.uncharted.terarium.hmiserver.models.dataservice.simulation; +import java.io.Serial; +import java.util.UUID; + +import org.hibernate.annotations.Type; + import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.databind.JsonNode; + import io.hypersistence.utils.hibernate.type.json.JsonType; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.ManyToOne; +import jakarta.persistence.SequenceGenerator; import jakarta.validation.constraints.NotNull; -import java.io.Serial; -import java.util.UUID; import lombok.Data; import lombok.EqualsAndHashCode; -import org.hibernate.annotations.Type; import software.uncharted.terarium.hmiserver.annotations.TSModel; import software.uncharted.terarium.hmiserver.models.TerariumEntity; @@ -29,6 +35,11 @@ public class SimulationUpdate extends TerariumEntity { @NotNull private Simulation simulation; + @Column(name = "sequence_number", nullable = false, unique = true) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_gen") + @SequenceGenerator(name = "seq_gen", sequenceName = "sequence_number_seq", allocationSize = 1) + private Integer sequenceNumber; + @Type(JsonType.class) @Column(columnDefinition = "json") private JsonNode data;