Selecting multiple objects by ID using JPA and ObjectDB?
I am having trouble getting ObjectDB to select multiple values based on their IDs. My request is very 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 identifier list contains a list of existing identifiers, all as long objects. I checked this three times.
Requests like:
entityManager.find(getEntityClass(), id);
... and ...
Query query = entityManager.createQuery("SELECT i FROM " + getEntityClass().getSimpleName() + " i", entityClass);
... works great.
Also, if I do this: entityManager.find (getEntityClass (), 1L);
I get the correct result: one 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 a variable values
.
What am I missing? Is this something that ObjectDB simply doesn't support?
Thank!
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. Unless it is trying to use the latest ObjectDB. If you get 1 as a result, try checking what is different in your application.