In my experience, the right path is in the middle: depending on your use case (for example, business logic), you will want one or the other.
An example of using identifiers: suppose a credit certificate wants to approve or reject a loan. For this, I would definitely go for ID: ApprovalService.approveCreditRequest (Long creditRequestId, ApprovalStatus approvedStatus); The problem of getting CreditRequest in the web layer seems to be a useless call for me, and suppose this is not a problem, you need to make sure that on your EJB / Service Layer the web layer will send you the correctly loaded CreditRequest , i.e. none of the web layers changed any important fields, for example creditRequest.setApprovalStatus(ApprovalStatus.DENY) or creditRequest.setRequester(anotherRequester) . And don't just think about WebLayer, as there might be Remote Ejb that invokes your level of service that you have to trust. Another use case where you definitely want to use identifiers is in the following scenario: you have a package in which you approve many (tens of thousands) of CreditRequests by calling the approveCreditRequest() method. Getting all entities from a database with all its relationships (and not just their identifiers) is probably a performance bottleneck. And passing each instance to the method can approve certain credit requirements twice (for example, between the time the instance was retrieved and approveCreditRequest() is approveCreditRequest() , it has already been approved).
The disadvantage of this method: it is not so simple UnitTest , you will need an integration test or a mocking structure (for example, mockito ).
An example of using completely objects: Suppose you need to change 90% of the fields after an HTTP request, for example, when the object is updated. The easier it is to work with all objects.
Now this has to do with the managed state of the instances: when the Entity instance leaves the service level (regardless of EJB or Spring Beans), you must be sure that the transaction is closed (Transactional is never used in WebLayer), and therefore the objects will be unmanaged automatically, so you You can easily change them in WebLayer.
source share