Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate entity serializable

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.

  • User makes a request to getMyEntity(), and gets the empty MyEntity object
  • 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?

like image 712
VB_ Avatar asked Sep 11 '13 12:09

VB_


People also ask

Are hibernate entities serializable?

Hibernate does not require serializable at all.

Why hibernate entity is serialized?

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.

Can entity be serializable?

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.

Does JPA entity need to be serializable?

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.


1 Answers

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.

like image 121
axtavt Avatar answered Sep 28 '22 00:09

axtavt