2

I am wondering what is the best way to handle mapping of entity beans (JPA 2) to DTOs. Since you cannot use entity beans "directly" with GWT, you need to handle DTOs instead.

I have several entities with various relationships (OneToOne, OneToMany, ManyToMany with a join table etc). Initially i started converting all entities to DTOs by hand with the help of a class MyEntityTransform.java with methods like :

  static final public CarBean persistant2Bean(CarPersist) {
            return new CarBean(cartPersist.getId(), carPersist.getName(),
                    carPersist.getDescription());
        }

Other methods are : persistent2BeanCollection(...), persistent2BeanMap(...), bean2Persistent(...), bean2PersistentCollection(...)

That becomes a fastidious task when handling collections, especially when the same entity has references to several other entities;

I have been thinking about using the DOZER framework to handle the mapping between entities and DTOs. It is mentionned here : http://code.google.com/intl/fr/webtoolkit/articles/using_gwt_with_hibernate.html

However i am not sure how well it handles the various JPA mappings (manytomany for instance) and how much work it is to configure it in the dozer-bean-mappings.xml file. Also i guess this framework is intensively using reflection to perform mapping operations. Such approach is much slower than mapping performed "by hands", e.g. when i use the methods in my MyEntityTransform.java class.

What do you suggest ? i'm interested in everybody's experience handling JPA entities with GWT.

Thanks.

Celinio

http://www.celinio.net/techblog

Celinio Fernandes
  • 163
  • 1
  • 4
  • 12

2 Answers2

2

In first instance I would always prefer Dozer. When the DTO structure is the same as your entities, you can use Dozer with zero configuration by simply calling the map function. When your DTOs differ from your entities the configuration overhead is minimal. Simply look in the really good documentation.

When performance becomes an issue, I would prefer a code generator approach, but I would never write the mapping code by myself cause it can be very error prone.

mlegenhausen
  • 317
  • 2
  • 4
  • Thanks for your feedback. Not sure what i will do since performance might suffer because Dozer intensively uses reflection to map the entities to the DTOs. – Celinio Fernandes May 02 '11 at 04:28
  • Keep a look the Dozer FAQ: http://dozer.sourceforge.net/documentation/faq.html#dozer-perf. When Performance is really an issue you worry about I think you have to benchmark different frameworks before starting your project. – mlegenhausen May 04 '11 at 12:10
1

If you want to just include entities in you EJB or JPA module in your GWT module follow these steps. I found it out my own and It worked for me.

  1. Include your EJB module in GWT module's build path (you may have already done that)

  2. Now goto your entities package in EJB module (i will take it as "com.ejbproject.entities")

  3. Create a file named Entities.gwt.xml (<ProjectSourcePath>/com/ejbproject/entities/Entities.gwt.xml)

  4. File content should be

    <module>
        <source>com.ejbproject.entities</source>
    </module>

  5. Now include following fragment in your GWT project's <modulename>.gwt.xml file.

    <inherits name="com.ejbproject.entities.Entities"/>

  6. Now you can include Entities in your GWT client side and gwtCompile without any problem

  • 1
    While this helps GWT to understand the classes, it doesn't solve the OP's problem: JPA entities cannot be serialized by GWT. See http://stackoverflow.com/questions/6405481/entity-with-relationships-through-gwt-rpc-problem – Hank Nov 04 '11 at 08:46