Home » Support » Forum » Query by array element

Query by array element

#1

Hello,

I have a class as follows:

@Entity
public class MyEntity {
    boolean properties[]= new boolean[1024]; 
}

I would like to query objects with feature '42':

select me from MyEntity me where me.properties[42]=true

Is it possible?

Thank you!

#2

This query is not supported since you cannot access arrays in queries.

You can implement a method in MyEntity that wraps the array access operation and then use that method in the query. It is supported by ObjectDB but not JPA portable:

import javax.persistence.*;

public class T1654 {

    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.getTransaction().commit();
       
        Query query = em.createQuery(
            "SELECT me FROM MyEntity me WHERE me.isProperty(42)");       
        System.out.println(query.getResultList().size());
       
        em.close();
        emf.close();
    }
   
    @Entity
    public static class MyEntity {
        boolean properties[] = new boolean[1024];
        boolean isProperty(int index) {
            return properties[index];
        } 
    }
}
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login