## JPA 2 Eager Fetching on a Collection

Quick little JPA 2 issue that I faced today that I'm sure many other people have run into especially when starting out. The problem was and error message such as the one shown below when attempting to display the contents of a Set of other entities in a JSF page

The class 'org.eclipse.persistence.indirection.IndirectSet' does not have the property Foo

The page was using a data table to display the set and the error message is telling me that the "#{item.id}" call that I had made failed. Fortunately I quickly realized what the problem was. Earlier on I had changed a relationship from OneToOne to OneToMany and a OneToMany relationship is lazy loaded by default under JPA. The solution was to change the annotation on the relationship so that it fetched eagerly like this:

@OneToMany( fetch= FetchType.EAGER )

Now, when I come to display this set of objects in JSF page it will be present rather than simply being a place holder "IndirectSet" object. Of course there are draw backs to making a relationship eager like this the most obvious one being the additional memory used and the time taken to query for the other objects.

There are several strategies for avoiding detachment (which I might discuss later) so that items can be lazy loaded as needed but all of them feel like a bit of a kludge to me. In this case, fortunately, the overhead of making this relationship eager was pretty small.

0 #1 Neal 2012-10-09 12:14
I am going to try your solution for my enigma:

Refresh

• #### Editing Null Data Values in a Cell with JavaFX 2

I would have never been able to find this out. Spent more than a week to even understand whats going ...

• #### Moving Picasa to a New Computer

I have to thank you for the efforts you have put in penning this blog. I really hope to view the same ...

• #### Editing Null Data Values in a Cell with JavaFX 2

I liked Harald K's suggestion, but I had to change it to: super.updateItem(item, empty && (getTableRow() ...

• #### Editing Null Data Values in a Cell with JavaFX 2

This was driving me absolutely crazy! Makes perfect sense now that it's been explained, of course...