EJB design: Which is the recommended approach to access data from web tier?
Hi, We are implementing a J2EE 5 application. In the Business tier we are using session beans and entity beans. Entity bean persistence has been resolved using JPA. We actually are a bit confused on deciding how to expose "data" to the web tier. For instance we have a use case in the system that must retrieve all the customers, along with its orders, and display them in the user interface. In order to solve this particular requirement we think of various alternatives: - Expose the entity beans directly. We don't what this alternative since we have business specific logic on the entity beans that we don’t want to expose to the web tier. - Use EJB-QL to build to build datatypes (using the NEW keyword) for each of the customers and return that list of datatypes to the web tier. The JPA specification don’t allow to use the NEW keyword inside the NEW operator, since we can’t build a datatype a customer datatype that contains all its orders. In order to build it, the datatype should have a constructor that receives all the customer attributes and perform an additional query to construct the list of orders. This particular approach implies dependencies from the data types to the entity beans, and also requires the datatype to perform lookups of the order entities. - Use separate queries, using the previous method. We have one query that get all the customers and for each customers we use an additional query to get its orders. In this case we are a bit concerned about the performance of this particular method, since it must do various queries for retrieving the data. - Have two different JPA mappings, one for the entity beans and one for the datatypes. The principal drawback of this approach is that we have to maintain both mappings. Which is the recommended mechanism to solve this type of use cases (i.e. those that must expose to the web tier the information contained in one o various entities) ? Thanks, Leo
- Posted by: Leonardo Rodriguez
- Posted on: June 05 2007 10:34 EDT
- Re: Which is the recommended approach to access data from web tier? by Anil Kumar Sadineni on June 05 2007 13:21 EDT
- Use DTOs by Johan Roch on June 06 2007 19:05 EDT
- Re: Which is the recommended approach to access data from web ti by J Dev on June 06 2007 23:48 EDT
I prefer first option. If you don't want to expose business logic related methods to presentaiton layer, create an interface per EJB and expose only setter/getter methods required for presentation layer. BTW, what kind of business logic are you keeping inside entity beans? Anil.
I dont know much about JPA yet, but as far as I know it relies on tools like Hibernate. In a layered architecture where your web-tier runs inside a separate JVM(is it the case?), I don't think this is a good idea to expose your pojos directly. There are big chances that you will get some kind of lazy-loading exceptions, because your entities will be detached from the session.I think the simplest way is to use some DTOs between your business tier and web tier. BTW, I think it is not bad to have some business logic inside your entities. We tend to forget that Java is OO(anemic model). To me session beans should only act as a facade...
Hi... I had the same question, how to expose our domain model entities to presentation layer, and how to pass data from presentation layer to persistense (DTO ?). leeme giv a example,consider when user creates a new product, and presses submit, how to pass tht product information to persistense layer. I hav dis question wid ejb only, here is how we do this in webwork + hibernate we use modeldriven actions, domain objects are exposed directly to views, whenever user creates a product and submits, webwork will create a new product object automatically (don ask me, how ?) set all its properties, and make it available in action, now every thing is easy, make this object persistent using productservice, Infact exposing domain objects directly to presentation layer has some advantag, (if i m not wrong), - U can utilize hibernate's Lazy loading feature , - Entire object graph will be availabe while rendering UI, (Open session in view) and many more, But wht abt ejb, is is gud to do dis wid entity beans? Sudhir nimavat jYog.com