Some other approaches mentioned we used based on JPA inheritance (@MappedSuperclass
) and/or database views and examplified with the following pseudo code (it works both ways for EAGER->LAZY or LAZY->EAGER scenarios):
@Table( name = "table_x" )
@Entity
class DaoX { @...( fetch = FetchType.EAGER ) refY ; /* ... other stuff ... */ }
via inheritance
so if we would like to have it lazily loaded we could pimp the inheritance hierarchy up like this with only minimal additional code:
@Table( name = "table_x" )
@MappedSuperclass
class abstract BaseDaoX { /* ... other stuff ... */ }
@Entity
class DaoX extends BaseDaoX { @...( fetch = FetchType.EAGER ) refY ; }
@Entity
class DaoXLazy extends BaseDaoX { @...( fetch = FetchType.LAZY ) refY ; }
so you can use DaoXLazy
or DaoZLazy
for your desired scenarios.
via views (in LAZY->EAGER scenarios)
(if you can change your current EAGER
to LAZY
which is generally more appropriate)
you could just map your (maybe deeply nested) lazy stuff with minimal load e.g. like this ( we like to load prop_f1
and prop_b1
here )
-- db view:
create or replace view view_x_eager as
select
tx.*,
f.prop_f1,
b.prop_b1
from table_x tx
-- assuming tx:f ~ 1:1 and f:b ~ 1:1 for simplicity here:
left outer join table_foo f on ( f.id = tx.foo_id )
left outer join table_bar b on ( b.id = f.bar_id )
@Table( name = "view_x_eager" )
class DaoXEager extends BaseDaoX {
@...( fetch = FetchType.EAGER ) refY ;
String prop_f1 ;
String prop_b1 ;
}