JPA 1.0: How to write an efficient transformer from an entity to a transfer object?

StackOverflow https://stackoverflow.com/questions/7528122

  •  26-01-2021
  •  | 
  •  

سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top