0

I have @ManyToMany or @OneToMany associations on entity and I'm considering what will be the better use case:

  1. using iterate for each object in objects and do criteriaBuilder.isMember(object, collection)

  2. using root.join(collection) and than joinedRoot.in(objects)

Here code example:

 List<Predicate> orPredicates = new ArrayList<>();

 for(Industry industry : industries) {
    orPredicates.add( criteriaBuilder.isMember(industry, provider.get(Provider_.industries)) );
 }

 predicates.add( criteriaBuilder.or(orPredicates.toArray(new Predicate[] { })) ); 

and the second possible solution

 Join<Provider, Industry> industryRoot = provider.join(Provider_.industries)
 industryRoot.in(industries); 
 // here i need also to add .distinct(true) on select 

I consider whether .isMember() function is making dome join with associated entity through collection attribute?

Gaurav
  • 1,549
  • 2
  • 15
  • 31
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • Have you tried both with sql statement logging enabled in your application? – carbontax Sep 17 '15 at 13:54
  • no i haven't ;/ do you think I should compare generated SQL queries? – Michał Ziobro Sep 17 '15 at 14:12
  • Yes, because it may turn out that Hibernate is generating identical or similar SQL from either query. It is worth inspecting the sql if you are finding the query is turning out to be expensive. (Or if you are just worried that it might not scale). http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values – carbontax Sep 17 '15 at 17:40

0 Answers0