275 words

Get multiple entities by Id

#1
2012-09-24 18:07

I'm trying to fetch a list of entities, by their Id. I Use this query:

SELECT c FROM User c WHERE c.id IN :ids

This querey works as long as I don't set the property "id" as @Id.

If i set @Id to any other member, this query works, if I set id as @Id, it stops working and gives zero results. Is this a known limitation or a bug? Do you have any idea how to solve this problem?

andix
andix's picture
Joined on 2012-09-24
User Post #1
#2
2012-09-24 18:10

Just found out thath it seems to work correctly with 2.3.7 and fails with 2.4.3_03

andix
andix's picture
Joined on 2012-09-24
User Post #2
#3
2012-09-24 22:41

Could you please post a sample test case that demonstrates the problem?

Something simple in this format will be very helpful.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #1,270
#4
2012-09-25 21:02

I could reproduce the problem with this test case:

import java.util.*;
import javax.persistence.*;
 
 
public final class demo {
 
    public static void main(String[] args)  {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory(
                        "objectdb:$objectdb/db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();
 
        em.getTransaction().begin();
        MyEntity e1 = new MyEntity("test1");
        em.persist(e1);
        MyEntity e2 = new MyEntity("test2");
        em.persist(e2);
        MyEntity e3 = new MyEntity("test3");
        em.persist(e3);
        em.getTransaction().commit();
 
        ArrayList<String> idsToRequest = new ArrayList<String>();
        idsToRequest.add("test2");
        idsToRequest.add("test3");
 
        Query query1 = em.createQuery("SELECT e FROM MyEntity e WHERE e.id IN :id");
        query1.setParameter("id",idsToRequest);
        List resultList1 = query1.getResultList();
        System.out.println(resultList1);
 
        Query query2 = em.createQuery("SELECT e FROM MyEntity e WHERE e.name IN :id");
        query2.setParameter("id",idsToRequest);
        List resultList2 = query2.getResultList();
        System.out.println(resultList2);
 
        em.close();
        emf.close();
    }
 
    @Entity
    public static class MyEntity {
 
        private String name;
 
        @Id
        private String id;
 
        MyEntity(String name) {
            this.name = name;
            this.id=name;
        }
        @Override
        public String toString() {
            return name;
        }
    }
}
andix
andix's picture
Joined on 2012-09-24
User Post #3
#5
2012-09-27 02:40

Thank you for the test case.

Actually the test passes also with ObjectDB 2.4.3, since this regression was added in build 2.4.3_01 (as a result of fixing another problem).

Please try build 2.4.3_04 that should fix the new problem.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #1,274

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel