0

i have 2 Entities with the following Metamodels.

@StaticMetamodel(SearchIn.class)
public class SearchIn_ {
    public static volatile SingularAttribute<SearchIn, Integer> id;
    public static volatile SingularAttribute<SearchIn, String> searchString;
    public static volatile SetAttribute<SearchIn, Take> takes;
    // Other Attributes...
}

@StaticMetamodel(Take.class)
public class Take_ {
    public static volatile SingularAttribute<Take, Integer> id;
    // Other Attributes...
}

The relationship is a ManyToMany mapped by SearchIn.Class and Take.Class has no referene to SearchIn.Class.

What i need now is all Takes that are mapped to a SearchIn with a specific searchString. I want to use the criteria api but i can't figure out which entitity i need as Root and how to do the joins with that. I saw some other questions that are similar but not realy what i want and i dont get it to work :/

So can somone give me a Hint and help me to build this up?

Dennis Ich
  • 3,585
  • 6
  • 27
  • 44

1 Answers1

2

Why don't you try a simple join:

Root<SearchIn> root = criteriaQuery.from(SearchIn.class);
Join<SearchIn, Take> takeJoin = root.join(SearchIn_.takes);
criteriaQuery.where(builder.equal(root.get(SearchIn_.searchString), "bla"));


TypedQuery<SearchIn> typedQuery = entityManager.createQuery(criteriaQuery);
return typedQuery.getResultList();

your return value will be a List of SearchIns. and you just call the getet for your Takes. I haven't tried if it's running. I just copied it from some of my code snippets.

tagtraeumer
  • 1,451
  • 11
  • 19
  • Hi, thanks for your answere my main problem seams to be that i didn't understood how to get thinks out of the criteria query and how it performs... Is it possible to get a Query that does not only get the Entity but also something aggregated? like how often a specific id was found to have that entry, that is the most important? – Dennis Ich Jun 24 '13 at 16:37
  • yes you could use a custom class instead of an entity: check the chapters "CriteriaBuilder's array" and "CriteriaBuilder's construct": http://www.objectdb.com/java/jpa/query/jpql/select even though object db is not always a good source for plain jpa/criteria api questions but this seems to be right and is very similar to what i use. – tagtraeumer Jun 26 '13 at 13:32