Stephan van Hulst wrote:To store objects in a relation database, queries must be executed. in JDBC, you write these queries yourself.
Hibernate writes the queries for you, but it must first know how to convert entities to data that it can store in a database. This is why it uses Java's serialization mechanism.
Actually, no. Hibernate/JPA does not use Serialization as part of the normal CRUD mechanism. But if you want to be able to transfer JPA Entities between JVMs, then they must be Serializable, just like any other transferrable object.
The JPA spec says:
JSR wrote:
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface.
It should be noted, however, that if you are intending to use JPA Entities as session-scoped objects in a web application, you
must make them Serializable, because
all session-scope objects must be serializable. And, I should add, that
you should detach them from the EntityManager between URL requests. Failure to do so is the equivalent of JDBC trying to pass a Connection between requests, and Connections are not Serializable (Connection is an interface, not a class), and thus can result in very unpredictable behavior.