1

I am new to SPRING and was assigned to work on project currently under development. Unfortunately development of the project has been slow so people have come and gone so I cant ask them why some things were done a certain way.

The project is a web service using SPRING.

They are using a View - Controller - Service (interface & implementation) - DAO (interface & implementation) - POJO (class used to transport data structure across layers).

Every POJO I have checked implementations serialization. On closer examination and search of the code, none of the POJO's are ever written or read, either in the POJO itself or any other file. Which has lead me to ask why its being done.

The POJO's are populated from Oracle statements in the DAO, which bubble upto the view, and then will bubble back down to the DAO where they information from them are written to the database using Oracle statements. The POJO itself is not written into the database.

Does SPRING MVC or java web applications require serialization and it is being used in the background? Is it needed to transmit the data between server and client connections? Is there a good reason that all the POJO's are using it that someone new would not recognize?

Fering
  • 322
  • 2
  • 18
  • Spring MVC end/or java web applications don't require serialization. I guess they wrote the code in that way in order to have all serializable object – Angelo Immediata Nov 07 '18 at 17:41
  • @AngeloImmediata Is there a reason why they would want that? Some design principle, carry over from old practices, ...? – Fering Nov 07 '18 at 17:54

3 Answers3

1

Depends on technologies used in the layers as well as implementation details.

If persistence is done using JPA/Hibernate then POJOs most likely will need to be Serializable.

In case if the POJO is passed to view via servlet session and session replication is on then you need to have your POJOs Serializable.

tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • Why would JPA entities need to be serializable? – chrylis -cautiouslyoptimistic- Nov 07 '18 at 17:58
  • @chrylis https://stackoverflow.com/questions/2020904/when-and-why-jpa-entities-should-implement-serializable-interface – tsolakp Nov 07 '18 at 18:05
  • That question doesn't actually have any correct answers. The accepted answer is nonsense. – chrylis -cautiouslyoptimistic- Nov 07 '18 at 19:13
  • The point of the link is not the accepted answer but fact that you can potentially run into not serializable entity exception with Hibernate and it has become a practice to mark JPA entities as serializable. – tsolakp Nov 07 '18 at 19:50
  • So, we got a new employee on the team and he happened to explain why, and your comment about how the java classes needed to be accessed by the front end view was why they were being serialized which is your third point, but I failed to pick up on it – Fering Nov 05 '19 at 20:18
0

Use of Java's default serialization is a normal way for regular POJOs.

Java specifies a default way in which objects can be serialized. Java classes can override this default behavior. Custom serialization can be particularly useful when trying to serialize an object that has some unserializable attributes.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I actually used that article to perform my searches to see if they were actually being read or written anywhere. – Fering Nov 07 '18 at 17:52
  • @Fering you will add serialization if you can have circular (object as a member of itself) serialization for example – Ori Marko Nov 07 '18 at 17:57
  • I went and looked, but I dont see any circular serialization happening. It was a quick look so I might have missed something, but all I am seeing is that the class is used in a list to represent the database information, is sent up to the view, and then brought back down and put into the database if something was changed. – Fering Nov 07 '18 at 18:05
0

This might not be the correct answer, but so far in my case it matches and explains what I am seeing. I have not seen this information mentioned else where, but the answer is well upvoted, has been around for awhile, and is from a high reputation user, so I am inclined to trust it.

There is an answer from another question where they mention something important.

As to the why you need to worry about serialization, this is because most Java servlet containers like Tomcat require classes to implement Serializable whenever instances of those classes are been stored as an attribute of the HttpSession. That is because the HttpSession may need to be saved on the local disk file system or even transferred over network when the servlet container needs to shutdown/restart or is being placed in a cluster of servers wherein the session has to be synchronized.

The application Im working on DOES use Tomcat, so if this is a restriction or behavior, then I can easily see why all the POJO's are created in this fashion, simply to avoid issues that might develop later, and is a result of experience having worked with this all before, and its that experience that I am lacking.

Fering
  • 322
  • 2
  • 18