英文:
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
之后,我想创建下一个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<Human> list = new ArrayList<>();
list.add(new Human("Mike", 1));
list.add(new Human("Lisa", 2));
list.add(new Human ("Monica", 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 + ": " + 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
好的,以下是翻译好的部分:
-
如果我将15个元素添加到我的列表中(ID从1到15)
-
然后我删除12个元素(现在只剩下ID:2、7和13)
-
对其进行序列化,然后在另一个主程序中进行反序列化
-
现在,当我在另一个主程序中创建新对象时,我想要的下一个ID是16,而不是1、2(重复)、3...等
如何实现这个要求?
英文:
Ok, it works well. But what if:
-
I add to my list 15 elements. (id from 1 to 15)
-
Then I delete 12 elements (now I have only id: 2, 7 and 13)
-
Serialize it and deserialized in other main
-
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论