måndag 19 januari 2009

Nice util to copy properties!

Have you ever used for instance JPA over EJB and got frustrated when you manually need to set your getters and setters of the DTO manually? I certainly have and in my current project we developed our own tool to handle these kind of problems.

Database (entity obejct) <-> DTO (transfer object) <-> EJB cloud <-> UI that uses DTO.

This is probably a common way to implement an application that takes advantage of Javas EJB but it also means A LOT of set and get between the DTO and the database entitys when you need to transfer data from backend to frontend.

Our first solution was a simple one, we used reflections to find getters and setter and then just transferred the values. However problem arised when we had lists with complex types i.e. DTO's in DTO's. Our copy properties class util just grow bigger and the performance was terrible!

One day we found this lovely tool on sourceforge called Dozer, Dozer homepage. Out of the box it handles all of these problems without any problem and it perform great! I made a quick benchmark with 10000 entitys and found it 8.6 times faster than our own implentation :)

It works like this. Lets say we have our entity object DogEO and our transfer object DogDTO:



DogDTO dto = new DogDTO();
dto.setHeight = 2;
dto.setWeigth = 23;

DogEO dogEo = new DogEO();

MapperIF mapper = new DozerBeanMapper();
dogEo = (DogEO) mapper.map(dto, DogEO.class);



This maybe seems like a small problem but trust me when you have 30+ DTO's with complex structure and content that are subject to constant change, then this is the shit!

onsdag 7 januari 2009

JPA Query cache

Ok i'll take this one in english since it doesn't touch any motorcycle stuff :)

I've recently had some cacheing problem with JPA when working with detached entities that we later on had to re-attache due to the fact that we are using EJB and hence have 2 separated domains.

In our case we actually don't want any cacheing under many circumstances since this put alot of old information into the entitymanager that is later fetched by other threads and hence they get old information. However it seems like many entitymanger providers have cacheing turned on by default.

I found an interesting blog article java.net which explains this problem pretty well, http://weblogs.java.net/blog/guruwons/archive/2006/09/understanding_t.html . This one is for those of you who are using toplink essentials, if you insted are using Kodo (BEA/ORACLE) as we do, please take a peek at http://edocs.bea.com/kodo/docs40/full/html/ref_guide_caching.html for more info on how to turn of the cache.

This seems to work very well but unfortunely you turn of ALL cacheing using this one, maybe not a wanted behaviour.

JPA XML Style:

>property name="kodo.QueryCache" value="false"/<