0

I have 2 little problems. Let's say I have an entity class called MyEntity. This class has two kinds of properties. Therefore I have two different class Property1 and Property2 which are also entity classes. There are bidirectional relations betweens MyEntity and the property classes, especially Property1 has an attribute List<MyEntity> owningEntities and Property2 has the attribute MyEntity owningEntity. Now I have the following query:

SELECT e FROM Property1 p JOIN p.owningEntities e WHERE p.name IN :propertyNames  

This query returns all entities that has a property of the first type with at least one of the given names. But in general the returned list is indistinct. The problem is that the SELECT DISTINCT downs't work here, at least with criteria API - I didn't try it with jpql. The code for the criteria query looks as follows:

CriteriaQuery<MyEntity> cq = cb.createQuery(MyEntity.class);
Root<Property1> property = cq.from(Property1.class);
SetJoin<Property1, MyEntity> entity =
                property.join(Property1_.owningEntities);
cq.where(property.get(Property1_.name).in((Object[]) propertyNames));
cq.select(entity);
// cq.select(entity).distinct(true); --> runtime error
List<MyEntity> resultList = em.createQuery(cq).getResultList();

Is there any way to get a distict result list? I can simply convert the list into a set, but it seems ineffective to extract the same results multiple times.

The next problem is, of course I want all entities that has a property of the any type with at least one of the given names. Until now I have two queries and afterwards I merge the results. But again I extract many results multiple times. So, is there any way to combine the following two queries in an effective way?

SELECT e FROM Property1 p JOIN p.owningEntities e WHERE p.name IN :propertyNames  
SELECT e FROM Property2 p JOIN p.owningEntity e WHERE p.name IN :propertyNames  

Thanks in advance!

SJuan76
  • 24,532
  • 6
  • 47
  • 87
Jogi
  • 295
  • 3
  • 11
  • How `propertyNames` is defined? – wypieprz Oct 10 '14 at 21:07
  • obviously it's an array... – Jogi Oct 20 '14 at 20:41
  • You should get compilation error at `SetJoin entity = ...` since `Property1` entity defines `List owningEntities` and the _static metamodel_ is used. – wypieprz Oct 21 '14 at 18:08
  • Maybe we should explain that to my compiler, he seems not to be aware of that... Jokes apart, the above code works without any error. Is your point that, it should be an ListJoin? I have no idea why, but the join method returns a SetJoin object, although owningEntites IS a list. – Jogi Oct 22 '14 at 15:05
  • Your compiler seems to be a badass as opposed to my therefore I am not able reproduce the problem. Using `Set` with `SetJoin` or `List` with `Listjoin` are preferred approaches ([how to choose a proper one](http://stackoverflow.com/questions/6562673/onetomany-list-vs-set-difference)) however I am not sure if it helps with this particular error. Could you please paste stack trace from runtime error anyway? – wypieprz Oct 22 '14 at 16:23

0 Answers0