问题描述
public class ExampleEntity implements Serializable
{
@Id
private long id;
private int fieldInt;
private long fieldLong;
private String fieldString;
}
我正在关注JPA教程。我能够理解基本概念,但在他们添加的所有教程中,这可序列化。这样做的目的是什么?这对我有什么帮助?
有什么建议吗?
I am looking after JPA tutorials. I am able to understand the basic concepts, but in all the tutorials they have added, this serializable. What is the purpose of this? How does that help me?Any suggestions Please?
推荐答案
这意味着该类可以被序列化。这意味着它可以转换为0和1的流并发送到其他服务,如Web服务或ORM(休眠)或其他任何服务。然后这个服务知道如何存储流。
It means that the class can be serialized. This means that it can be converted into an stream of 0's and 1's and be sent to other service, like a webservice or and ORM (hibernate) or whatever. Then this service knows how store the stream.
此外,你的程序可以接收一个序列化的类,并从0和1的流中为它创建一个实例。
Also, your program can receive a serialized class and create an instance for it from the stream of 0's and 1's.
这是保存类实例并在任何其他时间恢复它们的方法。
It's the way to "save" instances of classes and "restore" them at any other moment in time.
更多信息
- http://download.oracle.com/javase/6/docs/api/java/io/Serializable.html
- http://en.wikipedia.org/wiki/Serialization
为了能够使类可序列化,必须实现Serializable接口。
To be able to make a class serializable, you must implement the interface "Serializable".
-
为何如此?因为当序列化类的时刻到来时,将调用函数
writeObtject
将对象转换为字节。另一方面,当你有字节而你想获得类的实例时,将调用函数readObject
。
Why so? Because when the moment to serialize a class arrive, the function
writeObtject
will be called to convert the object into bytes. On the other hand, when you have the bytes and you want to get the instance of the class, the functionreadObject
will be called.
为什么不自动?它已经由字节表示的变量值。因为例如当一个类包含其他类的实例时会出现问题。你是做什么?你也序列化其他类?你只保留参考地址?它非常复杂,可能取决于您的项目和设计类型。
Why is not automatic? A variable value it's already represented by bytes. Because the problem comes for example when a class holds instances of other classes. What do you do? You serialize the other classes as well? You just keep the reference address? It's pretty complex and it may be dependant on your type of project and design.
这篇关于什么是Serializable?这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!