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
}