Business layer:
Persistence layer:
Web service layer:
The
User class from the business layer has no idea about the web service layer or the persistence layer. It is the primary class that models a user, and if you want to perform logic on a user, you will want to do it with this class. It may contain circular references: A
User references a
Company and the referenced
Company may refer back to the
User.
The
UserEntity class from the persistence layer is very similar to the
User class from the business layer. The main reason it's separate is because it's not easy to enforce class invariants on JPA entities. Therefore you only use it when you want to persist your user to the database, or when you want to retrieve it from the database. Immediately convert it to a
User object, and don't perform logic on it directly.
The
UserData class from the web service layer is a flattened object model that you can use to (de)serialize requests and responses to the web service. There are two major differences with the other two classes: First, it doesn't contain a reference to a company instance, because
you should not have circular references in your requests and responses. Secondly, it doesn't contain an ID that was generated by the DBMS. You must not expose such IDs to the outside world.