Results 1 to 3 of 3
  1. #1
    andjarnic is offline Member
    Join Date
    Nov 2010
    Location
    California
    Posts
    3
    Rep Power
    0

    Default Reusing pojos as entity beans and passing to servlets

    Hi all,

    I am trying to find a solution to making use of a pojo to pass around/reuse from the front end to the back end and vice versa.

    I have an EJB 3.1 entity bean, UserEntity. It has EJB 3.1 annotations in it, and a couple of properties, name, address, email, phone. I would like to find the "right way" to reuse the pojo so that I am not duplicating code over and over. For example, I have the UserPojo, but I also have UserEntity. They are basically the same thing, except that the UserEntity has annotations in it. To pass User data from the ejb tier to the servlet tier, I need some way to bundle up the user data. What I currently do is a lot of redundant copying of fields to fields. That is, something like:

    public void createUser(UserPojo user){
    UserEntity ue = new UserEntity();
    ue.setName(user.getName());
    ue.setAddress(user.getAddress());

    .. some code to persist the user ..
    }

    And in the opposite direction, some front end (servlet, REST resource, JMS listener, etc) requests a query of users. I get a Set of UserEntity objects, then I end up having to loop through them, creating new UserPojo objects, setting all the values on each of those objects from each of the USerEntity result objects, and so forth.

    So what I am trying to figure out is if what I am doing correct, or if there is a better way to use an object between tiers?

    One thought I had was.. and am not sure if I can do it.. is somehow in the back end have UserPojo, and some way to use AOP to add the EJB 3.1 entity annotations to it.. or perhaps just extend UserPojo from UserEntity and somehow add the entity annotations to it without duplicating all the UserPojo code?

    On the front end, I mostly deal with REST, so I have a UserResource that handles the requests from consumers, and I have a UserRepresentation class that extends UserPojo, but adds some REST specific extras. When a request comes in, it automatically handles parsing any xml/json into the UserRepresenation object for me. When I go to call the EJB side, I basically pass the UserRepresentation object as a UserPojo object so that the EJB only sees the UserPojo. What I'd like to avoid is having to do the get/set routine I do now with all the properties of the passed object to the ejb entity object. If I could also extend UserPojo so that I could simply pass a UserPojo instance and then simply persist it, instead of having to set all the properties onto a new instance of it, that would be great. Is there any mechanism like this, using AOP, CDI or such so I can avoid having to do the extra code of setting all the properties onto an entity bean that is pretty much identical tot he UserPojo except the EJB annotations added to it?

    Thanks.

  2. #2
    andjarnic is offline Member
    Join Date
    Nov 2010
    Location
    California
    Posts
    3
    Rep Power
    0

    Default

    Wanted to try to sum up what I said above... how do any of you pass data from the front to the back, be it in a JEE6 .war deployed ejb/servlet setup, or separated deployments of front-end and back-end ejb in different servers. Short of using a Map of Objects, I figured the best way was to use a common pojo, then derived the entity bean and any front end bean as well, from the common pojo, so that reusing is in place and copying values from one object to another is reduced or completely avoided due to the common pojo between the front/back end tiers.

    As an example, given any of the MVC frameworks, you have a user submit a form full of User data to register to a site. The front end, be it a servlet, Struts MVC, REST resource.. what have you there is some form of front-end "model" object modeling the form data sent in. On the back end, there is also some form of similar model object, be it an entity or otherwise, that is very similar, if not identical to the front end model. Because ejb annotations are not "visible" (nor should they be) to the front end, there should be some sort of intermediate object to use between the two tiers. Hence my UserPojo... if that makes sense. I was hoping there would be some fancy way of using this single UserPojo at the back end with EJB 3.1 entities, as well as passed to the front end (or vice versa) without having to actually do a bunch of setxxx(getXxx()) calls and extra object allocations.

  3. #3
    andjarnic is offline Member
    Join Date
    Nov 2010
    Location
    California
    Posts
    3
    Rep Power
    0

    Default

    Still nobody has any thoughts on how to do what I am asking about?

    I am playing around with it now, but not finding success yet on the entity front. To simplify my question.. is there some way to have the majority of the properties in a pojo, extend it and annotate it to make the extended class an entity? Something like:

    public class User {
    private String name;
    public String getName(){ return name;}
    public void setName(String name){ this.name = name;}
    }

    @Entity
    public class UserEntity extends User{
    .. some how annotate the inherited name property, or annotate an
    the methods by overriding them? ...
    }

    @Stateless
    public class UserBean {
    @PersistenceContext(...)
    EntityManager em;

    public void createUser(UserPojo user){
    UserEntity ue = (UserEntity)user;
    em.persist(ue);
    }
    }


    So basically, a UserPojo instance would be passed in to the stateless UserBean session bean, in the createUser() method. Simply typecasting it, then persisting it.. is that possible? I would generally think it should work, but have yet to try that (working on that now).

    I'll report back my findings.. but was hoping someone else might know off hand if something like this would work?

    The point of all this is to put the bulk of the "user" in the UserPojo class, and reuse it as an entity. As well, other classes extending UserPojo (in my front end) could be used interchangeably by typecasting. So passing a UserEntity as a UserPojo back to the front end, it could typecast the UserPojo to a UserSomething and not have to do any extra setXxx(getXxx()) calls to copy the properties of the returned object into a new UserPojo derived object.

    Thanks.

Similar Threads

  1. PK for Entity beans
    By eva in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 07-17-2008, 11:03 AM
  2. Entity Beans in JBOSS
    By dcpmt in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 11-29-2007, 03:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •