views:

29

answers:

1

Env: JBoss Seam, JPA, Hibernate

We use the @PostLoad annotation to dynamically initialize some of the transient variables in our @Entity (sometimes this involves looking up the parent entity to initialize status - which is a costly operation).

But under certain circumstances, we don't want this @PostLoad to get triggered, since we will not rely on the transient variables.

Is there a way to control the data dynamically post load.

One way to solve this issue, is to call this method only on demand (i.e. by removing the @PostLoad annotation and manually calling this method), but this is also error prone.

Are there any other ways to resolve this issue.

+1  A: 

But under certain circumstances, we don't want this @PostLoad to get triggered, since we will not rely on the transient variables.

Create two entities, one with the @PostLoad and the transient fields, and one "lighter" without.

Pascal Thivent
do you think that this is possible? since there can be only a single @Entity class for a particular table and if @PostLoad is declared it will run it. Not sure if we can have 2 different entity classes. Kindly clarify
Samuel
@Samuel Hmm... Who said *there can be only a single @Entity class for a particular table*? You CAN have several entities mapped on a same table. Now, I'm not saying this is ideal but in your case, this could be a solution.
Pascal Thivent
I agree that this could be a possible workaround, but it would involve additional classes and possibly higher code maintenance
Samuel
@Samuel: Actually, having several entities mapped on a same table was a common practice with Hibernate 2, when you couldn't use projections (so people were using a "light entity" with less attributes/associations for search screens and a "fat version" for detail screens). But you're right, this introduces extra maintenance.
Pascal Thivent