Hibernate serializable object

I read several topics about hibernating serialization and entities, but still cannot understand why my application works, even though I am not implementing Serializable.

"If an object instance is passed by value as a separate object, such as through a remote bean session business interface, the class must implement the Serializable interface." (c)

What I have: A simple Spring MVC project that uses Hibernate. I have MyEntity datatable and functionality to edit it by user from my application.

  • The user makes a request to getMyEntity () and receives an empty MyEntity object
  • Then in the form he sets all the necessary parameters

  • Then send the updated myEntity to the server

What is the problem: As far as I understand, the user will own the myEntity object when they fill out the form. So myEntity is in a disconnected state. BUT MyEntity DOES NOT IMPLEMENT Serializable

My questions:

  • Why does this work without using MyEntity Serializable?

  • I have the same scheme (described above) for all of my entities. Do I need to implement Serializable?

+6
source share
2 answers

Hibernate does not require objects to be Serializable .

"If an object instance is passed by value as a separate object, for example, through a remote bean session business interface, the class must implement the Serializable interface." (WITH)

This statement is a common source of confusion because it has nothing to do with Hibernate. In fact, this means that if you plan to use your objects in contexts where serializability is required, they must be Serializable .

Since Spring MVC does not require model attributes to be Serializable (unless you want to use session persistence for attributes stored in the session using @SessionAttributes ), so you don't have to worry about serialization in your case.

+8
source

Why does it work without MyEntity implements Serializable?

Hibernation does not require serialization of Entity. This is only required if you intend to send a separate object via a wire (for example, a remote interface)

I have the same circuit (described above) for all my objects. Do I have to implement Serializable?

Yes, if you want to send this object by posting. Otherwise, no.

+1
source

Source: https://habr.com/ru/post/953626/


All Articles