stewenson
stewenson

Reputation: 1322

JPQL - List of all items in collection member of JPA entity

I have this entity

public class A 
{
    private String id;

    private Set<String> categories;

    // getter and setter
}

I have this orm.xml

<entity class="A" access="FIELD">
    <attributes>
        <element-collection name="categories" fetch="EAGER"/>
    </attributes>
</entity>

I can not modify both class nor xml.

What JPA does is that it creates two tables, one for A entity, called A, and another table called A_categories.

That A_categories table has two columns, id column and "categories" column.

The question is, how do I get these all categories?

I tried to do it like this:

select a.categories from A a

but Hibernate says it is not an entity. I can not do it like "from A_categories" since it is not an entity either.

How to get list of all categories all A's are member of?

Upvotes: 0

Views: 1159

Answers (2)

kai
kai

Reputation: 6887

Query all A entities and add all their categories to one list.

List<String> categories = new ArrayList<>();

for(A obj : (SELECT a FROM A a))
    categories.addAll(a.getCategories());

Upvotes: 1

Kalpesh Soni
Kalpesh Soni

Reputation: 7287

query entity for A, lets call it a

just do a.getCategories()

Upvotes: 0

Related Questions