Search code examples
springjpaentitydaodto

Spring JPA - How to avoid heavy code redundancy


I started with smaller projects to learn how to use Spring. Actually I have a big problem.

Code Redundacy Maybe I missunderstood something but I have @Entity Classes to describe how my SQL Tables/Structure must be. On the otherside I have serializable classes. Example:

@Entity class UserEntity

and

class User implements Serializable

On CRUD operations I must transfer Values between this two. But why? This two classes are like the same for me. They have same members and getters/setters. Is there an elegant way to avoid this redundacy?

Maybe I do it completley wrong?


Solution

  • I would assume that your question is "Why do I need to make DTO for Entities? Isn't that redutant?"

    Simple answer : Safety reason.

    Complex answer :

    So there are some risk with JSP and MVC where if you put your managed entities into frontend, there are posibilities where you can inject data into database. Which is bad for site of course : )

    For more detail information check https://o2platform.files.wordpress.com/2011/07/ounce_springframework_vulnerabilities.pdf