Featured post
jpa 2.0 - Execute @PostLoad _after_ eagerly fetching? -
using jpa2/hibernate, i've created entity has uni-directional mapping entity x (see below). inside a, have transient member "t" trying calculate using @postload method. calculation requires access assosiated xs:
@entity public class { // ... @transient int t; @onetomany(orphanremoval = false, fetch = fetchtype.eager) private list listofx; @postload public void calculatet() { t = 0; (x x : listofx) t = t + x.somemethod(); } }
however, when try load entity, "org.hibernate.lazyinitializationexception: illegal access loading collection" error.
@ org.hibernate.collection.abstractpersistentcollection.initialize(abstractpersistentcollection.java:363) @ org.hibernate.collection.abstractpersistentcollection.read(abstractpersistentcollection.java:108) @ org.hibernate.collection.persistentbag.get(persistentbag.java:445) @ java.util.collections$unmodifiablelist.get(collections.java:1154) @ mypackage.a.calculatet(a.java:32)
looking @ hibernate's code (abstractpersistentcollection.java) while debugging, found that:
1) @postload method called before "listofx" member initialized
2) hibernate's code has explicit check prevent initialization of eagerly fetched collection during @postload:
protected final void initialize(boolean writing) { if (!initialized) { if (initializing) { throw new lazyinitializationexception("illegal access loading collection"); } throwlazyinitializationexceptionifnotconnected(); session.initializecollection(this, writing); } }
the way i'm thinking fix stop using @postload , move initialization code gett() accessor, adding synchronized block. however, want avoid that.
so, there way have eager fetching executed prior @postload being called? don't know of jpa facility that, i'm hoping there's don't know.
also, perhaps hibernate's proprietary api has control behaviour?
this might late, hibernate seems not support default jpa fetchtype option
@onetomany(orphanremoval = false, fetch = fetchtype.eager)
you must use hibernate specific one:
@lazycollection(lazycollectionoption.false)
- Get link
- X
- Other Apps
Comments
Post a Comment