users@glassfish.java.net

Re: Accessing EJB3 Entities remotely

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Wed, 26 Sep 2007 23:26:46 +0200

Consider using data transfer objects (known as value objects as well)
instead of serializing entities. You will get much more freedom. The
biggest advance is that, when you are thinking about database
structure (determined by @Entity objects) you are not tied up by
client requirements; for example: consider situation where you have
following entities:
Person, Company and Car.
Person can have many companies, company can have many persons and both
can have many cars.
Now, let's say you want to create a PersonForm, where you have:
person Id, first/last name, list of company names and vehicle count.
If you use @Entities as data transfer objects, you will have to
download entire person entity, all company entities and all vehicle
entities just to show first name, last name, company names and number
of vehicles... Now, each company has set of few persons, where each
person has few companies and both those companies and persons have few
cars each... In the worst scenario, you will download entire database
just to show one form with few fields :/
But you can create PersonDto with few fields like:
long id, long version, String firstName, String lastName, List<String>
firmsNames, int vehiclesCount.
In your session bean you can create method
PersonDto entity2dto(Person entity) {
  PersonDto dto = new PersonDto();
  dto.setFirstName(entity.getFirstname());
  dto.setLastName(entity.getLastName());
  @SuppresWarrning("unchecked")
  List<String> firmsNames = em.createQuery("SELECT c.name" +
    " FROM Company c WHERE ?1 MEMBER OF c.persons")
    .setParameter(1, entity)
    .getResultList();
   dto.setFirmsNames(firmsNames);
   Number vehiclesCount = (Number) em.createQuery("SELECT COUNT(p.vehicles) " +
     " FROM Person p WHERE p = ?1")
     .setParameter(1, entity)
     .getSingleResult();
   dto.setVehiclesCount(vehiclesCount.intValue());
   return dto;
}

I know it is a little bit verbose, but you can encapsulate all those
queries inside some data access object bean (behind PersonDAOLocal
interface with methods like getVehiclesCount(Person p)),
and now there is nothing that can stop you from sending whatever you
want to client.

In my application, we use something like the above, with dto2entity
methods that can merge changes made by user on DTO object.

Regards,
Witold Szczerba

2007/9/26, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> Hi Shri,
>
> You can get a hold of an EM on the client side by either injecting or looking up an EM or EMF (depending on the client type) or creating EMF (and then EM) via Persistence.createEntityMangerFactory (if neither injection, nor lookup is an option). You'll need to merge the entity to the client side EM to access the LAZY loaded fields.
>
> Regards,
> -marina
> [Message sent by forum member 'mvatkina' (mvatkina)]
>
> http://forums.java.net/jive/thread.jspa?messageID=237249
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>