I assume if you're using DTO and DAO, there is no need for entities, at least examples that i saw in this way. Or is it optional to have entities in this scenario ?
public interface CustomerResource {
@GET
@Path("/getCustomerListByUserID/{userID}")
Response getCustomerListByUserID(@PathParam("userID") String userID);
@DELETE
@Path("/deleteCustomer/{customerID}")
Response deleteCustomer(@PathParam("customerID") int customerID);
@POST
@Path("/updateCustomer")
Response updateCustomer(CustomerDTO customer);
}
public class CustomerResourceImpl implements CustomerResource{
@Override
public Response deleteCustomer(int customerID) {
internalService.deleteCustomer(customerID);
}
@Override
public Response getCustomerListByUserID(String userID) {
internalService.getCustomerListByUserID(customerID);
}
@Override
public Response updateCustomer(CustomerDTO customer) {
internalService.updateCustomer(customer);
}
}
public interface CustomerDAO extends BaseDAO<CustomerDTO> {
List<CustomerDTO> getCustomerListByUserID(String userID);
void deleteCustomer(Integer customerID);
void updateCustomer(CustomerDTO customer);
}
And internalService directly calls CustomerDAO
Are there anything wrong about this structure, how can it be better, is there any need for Customer entity?
Thank you so much ! Wish success for you all !
In JPA, Entities vs DTOs are two different projections that can be returned from your DAO or Repository. The difference is that Entities are managed (beans) whereas DTOs are unmanaged (simple data carriers). This makes an Entity a direct representation of a database where a DTO is just a message.
Note the "reference to a persistence layer" in modern Java is often handled through annotations.
Since people use these terms loosely and interchangeably, the JPA distinction is not always followed in conversation; but it is one clearcut way to separate definitions.
Related: