JPA 1.0: How to write an efficient transformer from an entity to a transfer object?
I want to write an efficient transformer, that will transform a JPA (1.0) entity to my transfer object and that:
- will be called in a transactional context,
- will cause no extra DB interaction, that is will transform only what's already loaded.
I cannot use the construct if (entity.getSomething() != null)
as "something" will get loaded.
The only thing I can think of is having the transformer spawn a new transaction, reload the entity in the new transaction's开发者_开发技巧 context and commit it — and then working on a detached entity. It does involve a reload, though. Any cleverer ideas?
You may use XStream to transform your Entity[Implicitly Serializable
] to XML & to de-serialize back to object. It may not be the effcient one, but fast & easy to implement.
I am successfully using the same process for a project.
ORM proxy stuff can be helped with using Gilead. http://noon.gilead.free.fr/gilead/
JPA provides a PersistenceUnitUtil class with an isLoaded() API that can be used to determine if a relationships is loaded.
精彩评论