ObjectDB ObjectDB

Issue #760: JPQL support for mapped by (inverse) collections

Type: Feature RequestVersion: 2.3.7Priority: NormalStatus: ActiveReplies: 0
#1

As discussed and demonstrated in this forum thread, collection operators and methods (IS EMPTY, SIZE, MEMBER OF, IN) are currently not supported for mapped by (inverse) collections.

These operations are supported for direct (not mapped by) collections. Navigation and JOIN is supported for both direct and mapped by collections.

The following test case demonstrates a problematic query and a workaround:

import java.util.*;

import javax.persistence.*;


public class T760 {

    public static void main(String [] args){
       
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:test.tmp;drop");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        B b = new B();
        em.persist(b);
        A a1 = new A();
        a1.add(b);
        em.persist(a1);
        A a2 = new A();
        em.persist(a2);
        em.getTransaction().commit();
       
        //Query query = em.createQuery(
        //    "SELECT a FROM A a WHERE a.list IS EMPTY");
        //System.out.println(query.getResultList().size());

        // SELECT a FROM A a WHERE a.list IS NOT EMPTY
        List<A> list1 = em.createQuery(
            "SELECT DISTINCT b.a FROM B b", A.class).getResultList();
        System.out.println(list1);

        // SELECT a FROM A a WHERE a.list IS EMPTY
        List<A> list2 = em.createQuery(
            "SELECT a FROM A a WHERE a NOT IN :list", A.class)
            .setParameter("list", new ArrayList(list1)).getResultList();
        System.out.println(list2);

        em.close();
        emf.close();
    }

    @Entity
    static class A {
        @Id @GeneratedValue long id;
       
        @OneToMany(mappedBy="a")
        ArrayList<B> list = new ArrayList<B>();
       
        void add(B b) {
            list.add(b);
            b.a = this;
        } 
       
        @Override
        public String toString() {
            return "A" + id;
        }
    }

    @Entity
    static class B {
        @Id @GeneratedValue long id;
       
        @ManyToOne
        A a;       
    }
}

The query in comment throws the following exception:

Exception in thread "main" [ObjectDB 2.x] SELECT a FROM A a WHERE a. ==> list <==  IS EMPTY
javax.persistence.PersistenceException
Invalid operand type com.objectdb.test.bug.forum.F399$B for operator IS EMPTY (error 756)
(position 26) at com.objectdb.jpa.JpaQuery.getResultList(JpaQuery.java:695)

The second query is a workaround for IS NOT EMPTY:

SELECT DISTINCT b.a FROM B b

And the third query (which is based on the second query) is a workaround for IS EMPTY (i.e. it may replace the first query):

SELECT a FROM A a WHERE a NOT IN :list
ObjectDB Support
edit
delete

Reply

To post on this website please sign in.