1

I have a Spring boot project that uses REST endpoints and some data model POJOs which are stored in a database via Spring repository classes. The service layer does almost nothing, instead it delegates the request to repositories and the data model. The model objects are not persisted in session or cached.

In this scenario, should the data model elements implement Serializable?

What is the best practice? Sample code,

public interface UserService {

    Optional<User> getUserByLoginName(String loginName);

    User findById(Integer id);

    User findByUsername(String username);

    User saveUser(User user);
}

Nothing special in implementation class either, just delegates the request to repository,

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
    Optional<User> findByLoginName(String loginName);
    Optional<User> findByName(String name);
    List<User> findByFilial(String name);
}

Model,

@Entity
@Table(name = "user")
public class User implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    @Column(nullable = false,unique = true)
    private String loginName;

    @Column(nullable = false)
    private String name;
    .....
    // getters and setters and rest of the fields
  }
Imran
  • 1,732
  • 3
  • 21
  • 46
  • 1
    would you please add more explanation and sample code to your question? I think this is a little obscure. – Hamid Ghasemi Apr 18 '18 at 17:10
  • 1
    When no serialization is performed, no serialization implementation is required. Short answer: no. – DwB Apr 18 '18 at 17:12
  • sample code is added. Probably the developer wanted to implement caching and did not mange to do so. I did not find any other good reason so wanted to check with the community as well. – Imran Apr 18 '18 at 18:41

1 Answers1

1

Here you can read about the purposes of using serialization in Java.

In your scenario, you are using spring repository to save and load entities to/from database and absolutely the entities don't need to be Serializable.

Hamid Ghasemi
  • 880
  • 3
  • 13
  • 32
  • 1
    I am aware of the usage of Serialization, and this code is not mine. When I saw that, I started to think about what was the reason for that, maybe there is some reason in that context that I am not aware of. Probably the developer planned to create caching but did have a chance, to do so. – Imran Apr 18 '18 at 18:39