Tuesday, January 15, 2008

wire up your relationships!

Again I got caught by not wiring up my ejb bidirectional relationships. As an example I had GeoFence (gf) and GeoFenceDataSet (ds). gf has one ref to ds and ds has lazy collection of gf's.

I never understood why I need to do a ds.getGeoFences().add(gf). I thought this should be magically looked after whenever I create a gf with gf.setDataSet(ds). In particular what I saw was, I would do the following within a transaction

em.persist(gf)
em.flush()

and then

for (gf : ds.getGeoFences() )

in a separate transaction. Since my collection was lazy-loaded, I expected the newly added gf to be in that collection, since it should have been loading the collection from the database, but it didn't. It looks like the entity manager knows whether to read in collection or not. It seems to keep track of the ds verison number and the last gf id it should know about.

No comments: