posted 15 years ago
I'm trying to study for the SCBCD exam and am working through some examples to try and get up to speed on JPA. I have some behavior that doesn't make sense to me and I'm hoping you guys can shed some light on it. I'm working through a lazy loading, one-to-many example. I have 2 classes, Fly and FlyBox. FlyBox has a one-to-many unidirectional relationship to Fly that is lazy loaded. Here are relevant snippets of each's code:
I have a stateless session bean that is retrieving a FlyBox and using a left fetch join to also retrieve the associated Fly records for a given FlyBox id. Here is the following Stateless session bean code:
My problem is, is that the number of results returned for a single FlyBox query are proportional to the number of Fly records that particular fly box has (and thus breaks this code). For instance, if I have a FlyBox that has 4 Fly records associated with it, the query will return that same FlyBox record 4 times. If I have a FlyBox with 2 Fly records, the above query attempts to return that FlyBox 2 times.
FYI, if I change the fetch type to eager and remove the fetch join portion of the query, everything is returned appropriately.
What am I missing?
Thanks in advance.