users@glassfish.java.net

Re: Relationship navigation in java code failing to get properly

From: <glassfish_at_javadesktop.org>
Date: Fri, 23 Feb 2007 18:10:51 PST

[i]/me shoots self.[/i]

Well after about 5 hours of hunting and testing it comes down to one line of code that was hosing me up.

I implemented a method to abstract looking up AssetClass objects by name:
[code]
    public AssetClass getAssetClass(String name) {
        
        // Create the query to get the asset
        Query query = em.createQuery(
            "SELECT DISTINCT ac FROM AssetClass AS ac " +
            "LEFT JOIN FETCH ac.properties " +
            "LEFT JOIN FETCH ac.relationships " +
            "WHERE ac.name = :name");
        query.setParameter("name", name);
        query.setMaxResults(1);
        
        // Get the entity, then return it
        AssetClass entity = (AssetClass) query.getSingleResult();
        return entity;
    }
[/code]

Turns out that when you're doing a JOIN FETCH, setMaxResults will cause, what in retrospect makes perfect sense: The relationships you JOIN FETCHed will only have a number of entities equal to whatever your setMaxResults was.

The reason for inconsistency was different code paths, one WOULD execute this code and populate the cache with the improperly limited results. Since the cache was hanging on to this data, it wouldn't fetch the proper list items. After redeploying and executing a different code path (a flawed aspect of my troubleshooting process that it took me a while to narrow down) everything worked as expected.

Sorry for wasting your time, but thank you for helping. My hope is that I can save someone else the trouble I was experiencing. You're all great, thanks again.
[Message sent by forum member 'jeffreyrodriguez' (jeffreyrodriguez)]

http://forums.java.net/jive/thread.jspa?messageID=205077