[ 
https://issues.apache.org/jira/browse/OPENJPA-521?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18142292#comment-18142292
 ] 

Richard Zowalla commented on OPENJPA-521:
-----------------------------------------

Still reproducible on current master (4.2.0-SNAPSHOT, 4bad79e2f) with Derby. 
Reproducer, e.g. in 
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/repro:

{code:java|title=UParent521.java}
package org.apache.openjpa.persistence.repro;

import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;

@Entity
public class UParent521 {
    @Id
    private int id;

    @OneToMany
    @OrderBy("name ASC")
    private List<UChild521> children = new ArrayList<>();

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public List<UChild521> getChildren() { return children; }
}
{code}
{code:java|title=UChild521.java}
package org.apache.openjpa.persistence.repro;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class UChild521 {
    @Id
    private int id;

    private String name;

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
{code}
{code:java|title=TestRepro521.java}
package org.apache.openjpa.persistence.repro;

import jakarta.persistence.EntityManager;

import org.apache.openjpa.persistence.test.SingleEMFTestCase;

/**
 * OPENJPA-521: with the data cache on, a unidirectional @OrderBy collection 
keeps its old order after a child's
 * ORDER BY field changes.
 */
public class TestRepro521 extends SingleEMFTestCase {
    @Override
    public void setUp() {
        setUp("openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", 
"sjvm",
            UParent521.class, UChild521.class, CLEAR_TABLES);
        try (EntityManager em = emf.createEntityManager()) {
            em.getTransaction().begin();
            UParent521 p = new UParent521();
            p.setId(1);
            em.persist(p);
            for (int i = 4; i > 1; i--) {
                UChild521 c = new UChild521();
                c.setId(i);
                c.setName("child" + i);
                em.persist(c);
                p.getChildren().add(c);
            }
            em.getTransaction().commit();
        }
        try (EntityManager em = emf.createEntityManager()) {
            em.find(UParent521.class, 1).getChildren().size();
        }
    }

    private String names() {
        StringBuilder sb = new StringBuilder();
        try (EntityManager em = emf.createEntityManager()) {
            for (UChild521 c : em.find(UParent521.class, 1).getChildren()) {
                sb.append(c.getName()).append(',');
            }
        }
        return sb.toString();
    }

    public void testRenameChildChangesOrder() {
        assertEquals("child2,child3,child4,", names());
        try (EntityManager em = emf.createEntityManager()) {
            em.getTransaction().begin();
            em.find(UChild521.class, 4).setName("child1");
            em.getTransaction().commit();
        }
        assertEquals("child1,child2,child3,", names());
    }
}
{code}

Result:
{noformat}
ComparisonFailure: expected:<child[1,child2,child3],> but 
was:<child[2,child3,child1],>
{noformat}
The bidirectional case (mappedBy, see TestOrderbyInDataCache) is handled: the 
parent's cached collection is cleared through the inverse relation. A 
unidirectional @OneToMany @OrderBy has no inverse, so the cached order stays 
stale.

> Collection ordering doesn''t work if the cache is enabled
> ---------------------------------------------------------
>
>                 Key: OPENJPA-521
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-521
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: datacache
>    Affects Versions: 0.9.7, 1.0.0, 1.0.1, 1.0.2
>         Environment: Kodo4.1.4 with OpenJPA1.0.0
>            Reporter: Xiaoqin Feng
>            Priority: Major
>         Attachments: openjpa-521-v2.patch, openjpa-521.patch, 
> openjpatestcase.zip, testcase.zip
>
>   Original Estimate: 168h
>  Remaining Estimate: 168h
>
> In a use case that has 2 persistence classes - parent and child.  Parent and 
> child is one-many relationship. 
> Parent entity has a collection of children.
> When order-by is set for collection of children and data cache is enabled, 
> inserting children, updating children's field value that is used by ordering 
> will cause collection out of order. But data cache doesn't aware this and 
> returned out of order collection.
> If data cache is disabled, it always use SQL query with "ORDER BY" applied so 
> collection is returned in correct order.
> Test case will be attached.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to