Skip to content

Commit

Permalink
fix: entity serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed May 29, 2023
1 parent b148d37 commit 574d94c
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ public CompletableFuture<T> load(UUID entityId) {
case MONGODB -> {
Document document = mongoDB.getCollection(collection).find(Filters.eq("entityId", entityId.toString())).first();
if (document != null) {
T entity = (T) document.get("entity");
loadedEntities.put(entityId, entity);
return entity;
String serializedEntity = document.getString("entity");
try {
T entity = (T) Utils.deserializeObjectFromString(serializedEntity);
loadedEntities.put(entityId, entity);
return entity;
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
throw new RuntimeException("No entity found with id: " + entityId);
}
Expand Down Expand Up @@ -163,12 +168,12 @@ public CompletableFuture<Void> save(Entity entity) {
String query = result ? "UPDATE " + collection + " SET entity = ? WHERE entity_id = ?;" : "INSERT INTO " + collection + " (entity, entity_id) VALUES (?, ?)";
try {
PreparedStatement statement = Objects.requireNonNull(connection).prepareStatement(query);
String serializedEntity = Utils.serialize(entity);
String serializedEntity = Utils.serializeObjectToString(entity);
statement.setString(1, serializedEntity);
statement.setString(2, entity.getId().toString());
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
} catch (SQLException | IOException e) {
throw new RuntimeException(e);
}
});
Expand Down

0 comments on commit 574d94c

Please sign in to comment.