为什么一个类实现了Serializable接口? [英] Why does a class implements Serializable interface?
问题描述
@Entity
public class Husband implements Serializable {
@Id
private int id;
private String name;
@OneToOne
private Wife wife;
}
@Entity
public class Wife implements Serializable {
@Id
private int id;
private String name;
@OneToOne(mappedBy="wife")
private Husband husband;
}
- 什么是
Serializable
从广义上讲? - 为什么一个类实现
Serializable
interface? - 为什么丈夫成员单独拥有@OnetoOne(mappedBy =妻子),但妻子成员没有@OnetoOne(mappedBy =丈夫)
- What is
Serializable
in broad term? - Why does a class implements
Serializable
interface? - Why does the husband member alone have @OnetoOne(mappedBy ="Wife"), but the wife member does not have @OnetoOne(mappedBy="husband")
推荐答案
-
从广义上讲,序列化是Java为开发人员提供持久保存状态的方式任何对象到持久性存储的情况。
Serialization, in broad terms, is the way Java provides developers to persist the state of any object to a persistent store.
如果开发人员想要由于某种原因将其编码类的实例持久化到后备存储,那么该类需要声明为实现Serializable。
If a developer wants that for some reason instance of his coded class should be persisted to a backing store, then the class needs to be declared as implementing Serializable.
上述代码表示丈夫和妻子之间的一对一关系。这基本上意味着每个妻子与一个丈夫有关,每个丈夫与一个妻子有关。 :-)同样在上述关系中,丈夫是[实体 - 关系条款]中的关系的主人,这就是为什么妻子说它被丈夫映射/关联到丈夫而不是相反的方式。这意味着丈夫识别其妻子,而不是相反。
The above code represents a One to One relationship between a Husband and a Wife. Which basically means that each wife is related to one husband and each husband is related to one wife. :-) Also in the above relationship, the Husband is the master of the relationship [in Entity-Relationship terms] and that is why Wife says that it is mapped/associated to Husband by the Husband and not the other way around. Which means Husband identifies its wife and not the other way around.
这篇关于为什么一个类实现了Serializable接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!