Using @Index for fields and filter more than one field with "and" predicate ends in empty result list.
public class IndexTest { @Entity @Indices({ @Index(members = {"children.name"}), @Index(members = {"children.age"}) }) static class Parent { @Embedded List<Child> children; } @Embeddable static class Child { String name; Integer age; } public static void main(String[] args) { Child child1 = new Child(); child1.name = "Foo"; child1.age = 42; Parent parent = new Parent(); parent.children = Collections.singletonList(child1); EntityManagerFactory emf = Persistence.createEntityManagerFactory("foo.odb"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); em.persist(parent); em.getTransaction().commit(); System.out.println(em.createQuery("select from Parent p join p.children c where c.age = 42", Parent.class).getResultList().size()); System.out.println(em.createQuery("select from Parent p join p.children c where c.name = 'Foo'", Parent.class).getResultList().size()); System.out.println(em.createQuery("select from Parent p join p.children c where c.age = 42 and c.name = 'Foo'", Parent.class).getResultList().size()); } }
This returns:
1 1 0
Please solve this problem. Using indexes is a common practice to fasten the query; but if you can't use these fields in logical expressions like "and" is a big problem.