I've read a few topics about hibernate & entity serialization, but still can't grasp why my application works despite of fact that I don't implement Serializable.
"If an entity instance be passed by value as a detached object, such as through a session bean’s remote business interface, the class must implement the Serializable interface." (c)
What I have: Simple Spring MVC project that use Hibernate. I have MyEntity datatable, and functionality to edit it by user from my application.
Then in the form he sets all needed parameters
Then send updated myEntity to the server
What the problem: As I understand, a user will possess a myEntity object when they're filling up the form. So, myEntity is in detached state. BUT, MyEntity DOESN'T implement Serializable
My questions:
Why does it work without MyEntity implements Serializable?
I have the same scheme (described above) for all my entities. Do I need to implement Serializable?
Hibernate does not require serializable at all.
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.
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. In practice, if our object is to leave the domain of the JVM, it'll require serialization. Each entity class consists of persistent fields and properties.
Do I have to use Serializable for JPA and Hibernate entities? Entities do not need to be Serializable. However, if you want to serialize them, you can safely implement Serializable.
Hibernate doesn't require entities to be Serializable
.
"If an entity instance be passed by value as a detached object, such as through a session bean’s remote business interface, the class must implement the Serializable interface." (c)
This statement is a common source of confusion, because it has nothing to do with Hibernate. Actually it means that if you plan to use your entities in contexts where serializability is required, they should be Serializable
.
Since Spring MVC doesn't require model attributes to be Serializable
as well (unless you want to leverage session persistence for attributes stored in session using @SessionAttributes
), therefore you don't need to worry about serializability in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With