Skip to content

bug: Updating to 4.3.1 causes existing databases to no open correctly #1162

Description

@DarkAtra

It seems like #1117 breaks support for databases created with 4.3.0 - at least when working with repositories. The following exception is thrown:

Caused by: java.io.InvalidClassException: org.dizitart.no2.collection.NitriteId; incompatible types for field idValue
	at java.base/java.io.ObjectStreamClass.matchFields(ObjectStreamClass.java:2207)
	at java.base/java.io.ObjectStreamClass.getReflector(ObjectStreamClass.java:2128)
	at java.base/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:658)
	at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2078)
	at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1927)
	at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2252)
	at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1762)
	at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:540)

and the database is rolled back to an earlier version. In my case this was luckily caught in a test as it seems to result in a wipe of the collection in the database.

Reproducer: https://github.com/DarkAtra/nitrite-java/blob/v4.3.1-nitrite-id-issue/potassium-nitrite/src/test/kotlin/org/dizitart/kno2/MyTest.kt#L75-L103

Activity

  1. changed the title [-]Updating to 4.3.1 causes existing databases to no open correctly[/-] [+]bug: Updating to 4.3.1 causes existing databases to no open correctly[/+] on Sep 23, 2025
  2. DarkAtra commented on Sep 23, 2025

    @DarkAtra
    ContributorAuthor

    I think this fixes the issue but it's probably worth adding a few more tests just to be sure:

    Subject: [PATCH] fix(gh-1162): ensure NitriteId serialization is compatible with previous versions
    ---
    Index: nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    diff --git a/nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java b/nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java
    --- a/nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java	(revision a8d05e7251784114488f6030609ca95ad0138a2e)
    +++ b/nitrite/src/main/java/org/dizitart/no2/collection/NitriteId.java	(date 1758645815272)
    @@ -17,7 +17,6 @@
     package org.dizitart.no2.collection;
     
     import lombok.EqualsAndHashCode;
    -import lombok.Getter;
     import org.dizitart.no2.exceptions.InvalidIdException;
     
     import java.io.IOException;
    @@ -41,25 +40,25 @@
      * @see NitriteCollection#getById(NitriteId)
      * @since 1.0
      */
    -@Getter
     @EqualsAndHashCode
     public final class NitriteId implements Comparable<NitriteId>, Serializable {
         private static final long serialVersionUID = 1477462375L;
         private static final SnowflakeIdGenerator generator = new SnowflakeIdGenerator();
     
         /** The underlying value of the NitriteId. */
    -    private long idValue;
    +    // WARNING: naming it idValue breaks java serialization for databases created with earlier version of nitrite
    +    private long _idValue;
     
         private NitriteId() {
    -        this.idValue = generator.getId();
    +        this._idValue = generator.getId();
         }
     
         private NitriteId(String value) {
    -        this.idValue = Long.parseLong(value);
    +        this._idValue = Long.parseLong(value);
         }
     
         private NitriteId(long value) {
    -        this.idValue = value;
    +        this._idValue = value;
         }
     
         /**
    @@ -115,21 +114,25 @@
             }
         }
     
    +    public long getIdValue() {
    +        return _idValue;
    +    }
    +
         @Override
         public int compareTo(NitriteId other) {
    -        return Long.compare(idValue, other.idValue);
    +        return Long.compare(_idValue, other._idValue);
         }
     
         @Override
         public String toString() {
    -        return ID_PREFIX + idValue + ID_SUFFIX;
    +        return ID_PREFIX + _idValue + ID_SUFFIX;
         }
     
         private void writeObject(ObjectOutputStream stream) throws IOException {
    -        stream.writeUTF(Long.toString(idValue));
    +        stream.writeUTF(Long.toString(_idValue));
         }
     
         private void readObject(ObjectInputStream stream) throws IOException {
    -        idValue = Long.parseLong(stream.readUTF());
    +        _idValue = Long.parseLong(stream.readUTF());
         }
     }
  3. anidotnet commented on Sep 24, 2025

    @anidotnet
    Contributor

    Could you please raise a PR for this fix? I'll do a quick rollout if all check passed.

  4. added theissue type on Sep 24, 2025
  5. added this to the 4.3.2 milestone on Sep 24, 2025
  6. DarkAtra commented on Sep 24, 2025

    @DarkAtra
    ContributorAuthor

    Could you please raise a PR for this fix? I'll do a quick rollout if all check passed.

    --> #1164

    I also ran the tests from my project against the version with the bugfix and everything looks good. Not sure if there are any edgecases you can think of that are also worth testing.

  7. added a commit that references this issue on Sep 25, 2025
  8. added 2 commits that reference this issue on Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions