Question

I am developing an application using the App Engine datastore and, since Many To Many relationships are not supported I thought to use other features of the database to help myself in writing the application.

I am, in fact, storing a list containing the IDs of the related objects inside every object I have to link with the other table.

Note: I am using Java and JPA for everything.

Here the class I use

@Entity
public class Book {
    @Id
    private Long id;
    @ElementCollection
    private List<Long> specsId = new ArrayList<Long>();
    // ... getters & setters & other properties
}

Now I'd like to query the database filtering all the books which have in the specsId a certain value (or a combination of certain values).

Right now I use Spring to manage the connection to the database so I have DAOs and Services which query the database using an entityManager.

So far I tried loads of queries, starting with inverting the "in" operator which obviously didn't work

select from Book b where (1, 2) in b.specsId

Or tried simply to use

select from Book b where b.specsId = 1

to see if it returned just the book which had at least the "1" in its specsId, but the query returned every book in the datastore.

Then I found another similar question here which suggested to use

select from Book b where 1 member of b.specsId

but that just threw error (No meta-data for member named b on class Book Are you sure you provided the correct member name in your query?)

So... Is there a way to do this using hand written queries or maybe using the functions provided by javax.persistence.EntityManager?

Thank you in advance

EDIT as suggested I tried also

select b from Book b where b.specsId in (list)

but gave me back all the elements in the Book table

Was it helpful?

Solution

Ok, I think I'm going insane. I tried every possible combination of queries and it worked like this:

select from Book b where 1 member of specsId

Notice that the only difference in the code I tried the first time is that I do NOT specify b.specsId but leave only specsId I still don't know why like this it works but not the other way around (it should be more specific), so if anyone is kind enough to explain the mistery for me will be highly appreciated!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top