Query Using Batch Reading For Lazy Loading Relationship Returns Wrong Results (Doc ID 1311192.1)

Last updated on MARCH 04, 2016

Applies to:

Oracle TopLink - Version: 11.1.1.1.0 and later   [Release: and later ]
Information in this document applies to any platform.

Symptoms

A query for a relationship mapping defined with batch reading would return invalid results, if changes had been performed on the object graph of the parent which had been used in the where clause for the query that returned the parent objects.

Entity Object Model
HouseBO has a OneToOneMapping to GarageBO
WindowBO has a OneToManyMapping to WindowBO

Problem Scenario

public void test() throws Exception {
    JpaEntityManager entityManager =
    JpaHelper.getEntityManager(persistenceContext);

    entityManager.getTransaction().begin();
    Session session =
        JpaHelper.getEntityManager(persistenceContext).getActiveSession();

    ReadAllQuery query = new ReadAllQuery();
    query.setReferenceClass(HouseBO.class);
    Expression filter = query.getExpressionBuilder();
    filter = filter.getAllowingNull("garage").get("name").equal("G1");
    query.setSelectionCriteria(filter);

    Expression expressionFetch = query.getExpressionBuilder();
    expressionFetch = expressionFetch.getAllowingNull("window");
    query.addBatchReadAttribute(expressionFetch);

    List< HouseBO > result = (List< HouseBO >) session.executeQuery(query);
    assertEquals("Incorrect House result set size.", 1, result.size());
    HouseBO house = result.get(0);
    house.setGarage(null);

    entityManager.flush();
    assertEquals("Incorrect Window result set size.", 3,
    result.get(0).getWindow().size());
}

Observed Behavior
The second assertEquals will fail because the batch reading query generated would incorporate the query that had been executed for the parent. This query would check for the garage but the association with the garage had been removed from the house and this change had been written to the database with entityManager.flush() before the detail query

SELECT
   t0.ID, t0.AENDERUNGSZEITPUNKT, t0.NAME, t0.LOESCHKZ,
   t0.VERSION, t0.FK_HOUSE_WINDOW
FROM HOUSE t1
     LEFT OUTER JOIN GARAGE t2 ON (t2.ID =t1.FK_GARAGE_HOUSE),
      WINDOW t0
WHERE ((t0.FK_HOUSE_WINDOW = t1.ID)
   AND(t2.NAME = ?))
bind => [G1]


Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms