Selecting multiple objects by id using JPA and ObjectDB?

Edy Bourne picture Edy Bourne · Aug 21, 2014 · Viewed 10.7k times · Source

I'm having trouble getting ObjectDB to select multiple values based on their ids. My query is super simple:

Query query = getEntityManager().createQuery("SELECT i FROM " + getEntityClass().getSimpleName() + " i WHERE i.id IN :ids", entityClass);
query.setParameter("ids", ids);
List<Object> values = query.getResultList();

But no matter what, this always returns an empty list.

The ids list contains a list of existing ids, all as Long objects. I triple-checked this.

Queries like:

entityManager.find(getEntityClass(), id);

...and...

Query query = entityManager.createQuery("SELECT i FROM " + getEntityClass().getSimpleName() + " i", entityClass);

...work fine.

Also, if I do: entityManager.find(getEntityClass(), 1L);

I get the result correct result: a single instance.

But:

List<Long> ids = new LinkedList<Long>();
ids.add(1L);
Query query = getEntityManager().createQuery("SELECT i FROM " + getEntityClass().getSimpleName() + " i WHERE i.id IN :ids", entityClass);
query.setParameter("ids", ids);
List<Object> values = query.getResultList();

returns an empty list to the values variable.

What am I missing? Is this something ObjectDB just doesn't support?

Thanks!

Answer

ObjectDB picture ObjectDB · Aug 21, 2014

It should work. Please try the following simple test:

import java.util.*;

import javax.persistence.*;

public class TestInIds {

    public static void main(String[] args) {

        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        em.persist(new MyEntity());
        em.persist(new MyEntity());
        em.getTransaction().commit();

        Query query = em.createQuery("SELECT e FROM MyEntity e WHERE e.id in :ids");
        List<Long> ids = new LinkedList<Long>();
        ids.add(1L);
        query.setParameter("ids", ids);
        List resultList = query.getResultList();
        System.out.println("result size: " + resultList.size());

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

    @Entity
    static class MyEntity {
        @Id @GeneratedValue
        private Long id;
    }
}

It should print 1. If it doesn't try the last ObjectDB version. If you do get 1 as output try to check what is different in your application.