Why does JPA not use the 1st level cache for this query?

15 views Asked by At

I've got the below two queries.These queries are more or less the same - the only difference is that the second query also does a left join on a collection to fetch that collection.

When I try to access the audio files of the entities loaded via the first query, I get an error. I would expect that the second query would set the audio files on the entities loaded in the first query, as those entities would be loaded into the 1st level cache.

Why is the second query not setting audio files on the entities already loaded via the first query using the 1st level cache?

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    CriteriaQuery<MqTrack> c0 = cb.createQuery(MqTrack.class);
    Root<MqTrack> mqTrack = c0.from(MqTrack.class);
    c0.select(mqTrack);
    c0.where(mqTrack.get("cid").in(cids));
    List<MqTrack> mqTracks = entityManager.createQuery(c0).getResultList();

    CriteriaQuery<MqTrack> c1 = cb.createQuery(MqTrack.class);
    Root<MqTrack> entity = c1.from(MqTrack.class);
    Join<MqTrack, MqAudioFile> join = (Join) entity.fetch(MqTrack_.audioFiles, JoinType.LEFT);
    c1.select(entity);
    c1.where(entity.get("cid").in(cids));
    entityManager.createQuery(c1).getResultList();
            
    return mqTracks;
0

There are 0 answers