3

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!

Edy Bourne
  • 4,229
  • 9
  • 42
  • 75
  • Works indeed fine in Hibernate/EclipseLink. I don't have ObjectDB at hands nor am I interested to, but you could try using a true `Set` instead, or wrapping the `:ids` inside parentheses like so `IN (:ids)`. That was also needed in an older Hibernate version which was bugfixed about 5 years ago. – BalusC Aug 21 '14 at 07:12

1 Answers1

5

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.

ObjectDB
  • 1,294
  • 7
  • 8
  • Thanks your code helped me find the issue: my code was passing a list of ids, but the items were `String`s, not `Long`s. I thought this was correct due to the fact I got this in a method with signature `List` ids, but apparently a `List` came instead (this comes through injection so its not caught at compile time). Now it is working perfectly, thank you! – Edy Bourne Aug 21 '14 at 16:54