1

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?

MaDa
  • 10,511
  • 9
  • 46
  • 84
  • 1
    Sounds like an EJB 1.0 anti-pattern to me. – duffymo Sep 23 '11 at 11:22
  • 1
    @duffymo Please don't stray away from the topic, I'm not saying the application in question is an engineering masterpiece. If I could get rid of TOs, I would, but it's not doable within the time I have. – MaDa Sep 23 '11 at 12:02
  • I doubt that the efficiency of creating the TO is the bottleneck in your app. Write the naive implementation, profile it, and find out if you need something better. – duffymo Sep 23 '11 at 14:09
  • @duffymo OK, I probably overexposed "efficient"; should be "lazy". My true reason for this is that the current transformer generates a lot of unneeded SELECTs that get deadlocked with RR locks - and there's no business case for these SELECTs, so they can be safely eliminated. – MaDa Sep 23 '11 at 14:15
  • This is another reason why I don't get the ORM fetish. It's more trouble than it's worth. – duffymo Sep 23 '11 at 17:35

3 Answers3

1

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.

Puspendu Banerjee
  • 2,631
  • 16
  • 19
  • Hmmm, I haven't thought about that, thanks. It seems to be the only way to work around any proxy logic that JPA tool may generate. Do you think so? – MaDa Sep 27 '11 at 07:31
  • No, it's not the only way. I am working on an another project where we are planning a more efficient way & let you if we become successful. Stay in touch. – Puspendu Banerjee Sep 27 '11 at 17:57
1

ORM proxy stuff can be helped with using Gilead. http://noon.gilead.free.fr/gilead/

MarianP
  • 2,719
  • 18
  • 17
0

JPA provides a PersistenceUnitUtil class with an isLoaded() API that can be used to determine if a relationships is loaded.

James
  • 17,965
  • 11
  • 91
  • 146
  • I was asking SPECIFICALLY for JPA 1.0 - I know that 2.0 offers a lot more in terms of controlling and diagnosing entity states and persistence context. I cannot switch to 2.0, though. – MaDa Sep 27 '11 at 21:28