Java序列化和对象的唯一ID

huangapple go评论67阅读模式
英文:

Java Serialization and unique ID of objects

问题

我有一个类,每个对象都有自动生成的ID(实例编号)。我需要对其进行序列化,然后反序列化。很简单。
但问题是,在反序列化并创建新对象之后,这个新对象的ID应该高于序列化之前对象的最高ID。

例如:
我有一个包含3个Human(姓名,自动生成的ID)的List:

List<Human> list = new ArrayList<>();
list.add(new Human("Mike", 1));
list.add(new Human("Lisa", 2));
list.add(new Human("Monica", 3));

然后将List进行序列化,然后在Main2中进行反序列化。
之后,我想创建下一个Human,这个下一个Human应该具有ID 4。

如何让反序列化的List知道最高的ID是多少?

我尝试将最大的ID存储在静态字段中,但我无法在序列化后访问它(静态字段默认为transient)。

英文:

I have class that have automatically generated id (instance number) for every object. I need serialized this, then deserialized. Simply done.
But the thing is that after deserialization and create new object, this new object should have id higher than highest id from objects before serialization.

For example:
I have List of 3 Human(name, automatically generated id):

List&lt;Human&gt; list = new ArrayList&lt;&gt;();
list.add(new Human(&quot;Mike&quot;, 1));
list.add(new Human(&quot;Lisa&quot;, 2));
list.add(new Human (&quot;Monica&quot;, 3));

Then serialized List<Human> list and deserialized in Main2.
After that I wanna create next Human and this next Human should have id 4.

How to let know for deserialized List what's the highest id ?

I tried to store max id in static field but I can't access it after serialization (static is default transient)

答案1

得分: 0

您需要创建一个readObject方法来执行一些反序列化后处理操作。我还建议将id字段设置为transient,因为如果您打算替换它,就不需要对其进行序列化。

public class Human implements Serializable {
    private static final AtomicLong nextId = new AtomicLong(1);

    private transient long id;
    private String name;

    public Human(String name) {
        this.name = name;
        this.id = nextId.getAndIncrement();
    }

    public long id() {
        return id;
    }

    public String name() {
        return name;
    }

    public String toString() {
        return id + ": " + name;
    }

    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        id = nextId.getAndIncrement();
    }
}

请注意,我从构造函数中移除了id参数,这样可以防止创建具有随机id的人物。

英文:

You'll need to perform some deserialization post-processing by creating a readObject method. I'd also make the id field transient, as there's no need to serialize it if you're going to replace it.

public class Human implements Serializable {
    private static final AtomicLong nextId = new AtomicLong(1);

    private transient long id;
    private String name;

    public Human(String name) {
        this.name = name;
        this.id = nextId.getAndIncrement();
    }

    public long id() {
        return id;
    }

    public String name() {
        return name;
    }

    public String toString() {
        return id + &quot;: &quot; + name;
    }

    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.	defaultReadObject();
        id = nextId.getAndIncrement();
    }
}

Note how I removed the id parameter from the constructor; that prevents creating humans with random ids.

答案2

得分: 0

好的,以下是翻译好的部分:

  1. 如果我将15个元素添加到我的列表中(ID从1到15)

  2. 然后我删除12个元素(现在只剩下ID:2、7和13)

  3. 对其进行序列化,然后在另一个主程序中进行反序列化

  4. 现在,当我在另一个主程序中创建新对象时,我想要的下一个ID是16,而不是1、2(重复)、3...等

如何实现这个要求?

英文:

Ok, it works well. But what if:

  1. I add to my list 15 elements. (id from 1 to 15)

  2. Then I delete 12 elements (now I have only id: 2, 7 and 13)

  3. Serialize it and deserialized in other main

  4. Now when I create new Object in other main I wanna have next id 16, not 1,2(duplicate),3...etc

How to implement this?

huangapple
  • 本文由 发表于 2023年6月26日 22:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定