Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to duplicate jpa entities for business objects?

Tags:

java

I have a design/architectural problem:

I've started developing a java web application. I thought of using 3 layers: a persistence layer (with jpa and hibernate), a business layer and a presentation layer. My problem now is: the jpa entities would make the model but can or may I use the entities as business objects?

Is this a practice? My common sense says I shouldn't, but then, I need to duplicate these entities as business objects?

Finally, I'd like the presentation layer to be really decoupled from the other layers. While using spring mvc with jsp at first, I'd like. if it's suitable, at some moment to switch to javascript-based application that communicates with the backend through rest requests.

like image 568
Alex Avatar asked Nov 01 '22 10:11

Alex


1 Answers

Yes, you can. Outside the persistence context, the JPA Entities are like simple POJOs. It is legal to use them in business code (actually, as hinted by JB Nizet, you usually ALWAYS use them in your business layer without DAO). If it is tightly related to the Entity, you can even add business logic into your JPA beans. Nevertheless, it will be harder to read and understand what the code does. But if you have a reason to do that - there is nothing illegal. It all comes down to software design practices and what you need most.

When you want to change your app into the REST-powered service, it is not difficult. You will have to change the Servlet you are currently running your app with for a JAX-RS or other framework Servlet which will handle HTTP requests in a REST manner for you. It is done in web.xml. Then, you will place your html-pages in any place, where it is accesible for the remote hosts, and connect them to your REST-service with the Javascript AJAX or sth. You should take care of CORS then.

like image 143
Artem Moskalev Avatar answered Nov 15 '22 04:11

Artem Moskalev