Avoiding DTOs in RPC

241 views
Skip to first unread message

Thomas Lefort

unread,
Apr 19, 2012, 11:46:07 AM4/19/12
to google-we...@googlegroups.com
I want to avoid having to use DTOs for transferring my Users with RPC.
Basically I have a number of rpc calls for my users, and based on the configuration and the user rights, etc... there is a number of fields I want to hide. My approach has been to create DTOs for each case. I was now thinking of using the JPA relationships and lazy loading instead. For instance I store additional information (bio, etc...) in an "Additional" object, with a onetoone relationship and lazy loading. So I guess based on the call I just need to load the field or not, instead of filling up a dedicated DTO each time I send a User with "holes".

Does that make sense? any one has cons on this idea? will RPC send empty fields or will it be more clever and just not add the fields that are null?

Thanks,

Thomas


Thomas Broyer

unread,
Apr 19, 2012, 12:05:07 PM4/19/12
to google-we...@googlegroups.com
I don't know JPA very well, but I think lazy-loading will get in your way here. What you need is to conditionally load the Additional object, and in the case you don't, you want the field to be 'null'. AFAIK, this is pretty easy with JPA.
That being said, maybe detaching your object from the session would be enough to have a 'null' in the field, and more importantly have the getter not throw any exception but return simply return the null; but that doesn't really change the fact that you'd better ask JPA to load the Additional object if you need it (so it could use a JOIN instead of 2 SELECTs for instance) than use lazy-loading (and calling the getter as a trigger to load the Additional object)

Jens

unread,
Apr 19, 2012, 12:38:35 PM4/19/12
to google-we...@googlegroups.com
I think GWT-RPC will skip null fields when building the payload. 

But I would better ask if your JPA entity is serializable so you can share it between GWT and your server. JPA entities will be enhanced by the JPA provider and lazy fields can typically only be accessed while your persistence session is active. If you close your session and then send your data to GWT, during serialization the getter for the lazy field will be called and it will result in an exception (enhanced JPA class trys to load the lazy data upon getter call, but database session is closed already). Maybe you also see SerializationExceptions because your JPA entities are enhanced and may contain additional code / class references that are not serializable.

How to solve this depends on your JPA provider:
I think OpenJPA lets you detach an entity which gives you back the original POJO (maybe lazy fields will be null then, documentation may help to find out). 
For Hibernate you can use something like Gilead (only supports Hibernate) which does pretty much what you want (Lazy fields will become null). 
EclipseLink does static or dynamic weaving to enhance the JPA entities and I haven't found a way to make them serializable for GWT-RPC.

So I wouldn't choose EclipseLink if you want to avoid DTO's ;-)

-- J.

Alfredo Quiroga-Villamil

unread,
Apr 19, 2012, 12:39:56 PM4/19/12
to google-we...@googlegroups.com
Additional thoughts below:

"My approach has been to create DTOs for each case. I was now thinking of using the JPA relationships and lazy loading instead"

I would recommend to go the "lazy loading" route in this case. I think having a unique DTO will make things easier to maintain instead of creating specific DTOs for each case. If you are using GWT-RPC and you are in this case populating the DTO's yourself, you can check the user's "role" for example and based on that, lazy or eagerly fetch what you need. If you are using hibernate, this can be accomplished in a few different ways. Your default configurations are typically lazy there.

In the UI, you will always receive the same DTO. There you should at that point either know the user's role or check based on the response you received. The fields that do not apply to the user based on role should then be null and you should check for that.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/chqJD0S9QZMJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.



--
Alfredo Quiroga-Villamil

AOL/Yahoo/Gmail/MSN IM:  lawwton


Sebastian Gurin

unread,
Apr 19, 2012, 2:49:57 PM4/19/12
to google-we...@googlegroups.com
In a project of mine I successfully use THE SAME classes both as hibernate model and as GWT client model class using simple GWT RPC for client-server comunication: A couple of things I remember are:

1) I needed to put my DAO model classes in gwt "client" package because I work directly with this in GWT "client mode" (they need to be translated to javscript).

2) use lazy="true" in collection attribute definitions. If not, the getCollection() will return a Collection implementation not supported by GWT RPC.

3) (obvius) be careful your pojos will support RPC valid types. careful while using other framework that enrich your model POJOS like hibernate does for collection attributes.

I don't know if this will be of use for you...

> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/iLFbBNp8rLAJ.


> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
>


--
Sebastian Gurin <sgu...@softpoint.org>

nalin...@googlemail.com

unread,
Feb 23, 2013, 8:29:03 AM2/23/13
to google-we...@googlegroups.com
The child DTO objects,the attributes lazily loaded are null.. when trying to access from the client side.
On server side i use hibernate.. and there i could get the child objects..
can someone tell me.. how to get those data in client side.. I could get only the parent DTO data which is returned by the RPC Async call....
I have the DTO with All attributes from model class without annotation.....

> To unsubscribe from this group, send email to google-web-toolkit+unsub...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages