Forums Register Login

JPA: @OrderBy bypassed by cache?

+Pie Number of slices to send: Send
It appears that the @OrderBy annotation on a @OneToMany LinkedList member of an @Entity does not work when another item is added last to the list by a @Stateless session bean. The next access (by later HTTP request to a @Stateless session bean) calling myEntityManager.find will see an unsorted list. Restarting the app server solves the problem. My guess is that the JPA/ORM engine keeps a cache and don't give a hoot about @OrderBy. Example:



The first request does a carModelLocal.create(carMakerId, name, year);

The second request does a CarMaker carMaker = carMakerLocal.find(carMakerId)); which returns em.find(persistentClass, id);

The resulting carMaker has the new CarModel last in the LinkedList, ignoring @OrderBy. Aarrgh!

As I said, the probable culprit is some cache in the JPA/ORM engine. I use Glassfish v2.

Is this a bug or a feature?

What would be a canoncial workaround? Adding "manual" sorting in getCarModels()? Or is there a better way?

+Pie Number of slices to send: Send
If you want to have the List ordered you need to maintain the order when you add to it. In your addCarModel() method in your object you can use Collections.sort() to fix the order after adding to it.

You could also use a TreeSet to automatically maintain the order, but this would not work with a LAZY mapping.

In JPA 2.0 there is also support for an OrderColumn which maintains the order in the database as define in the object model.

See also,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Ordering

To disable caching in TopLink Essentials (default provider in Glassfish 2), you can use "toplink.cache.shared.default"="false" in your persistence.xml.

You can also call refresh() on the object, to refresh it from the database.
+Pie Number of slices to send: Send
Thanks James! Crystal clear explanation.

It appears that the best solution in this case is to add Collartions.sort() to all business methods that inserts (and renames!) List members. Re-sorting on each access seems ineffective. Ditto disabling the cache or calling refresh().
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5746 times.
Similar Threads
Primary key / Forign Key
testing jpa - OneToMany
Filtering Associated Collections
@OneToMany Cascading deletion problem
How to avoid this hibernate jpa LazyInitializationException exception in Swing application?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:44:58.