0

I have Entities which contain Collections (OneToMany) which hold Person Entities. A person does not know to which Collection it belongs. I want to search for MyPersons entities that contain specific Persons. The problem is that I dont know how to search with criteria when I have a list of Persons that need to be mapped to the MyPerson Entities. Excuse me for my bad english. I hope someone can help.

Jason
  • 1
  • A little more context would be useful. What is the relation in between your entities especially MyPersons with Collections? Are you talking about database query or object filtering? – cool Sep 29 '20 at 12:03
  • I need to retrieve all MyPersons Entities from a database, where a MyPersons Entity contains a MyPerson Entitiy from an ArrayList. And I need to do it with the Criteria API if possible. – Jason Sep 29 '20 at 12:19
  • And I did a little mistake in my Question. A MyPersons Entity holds only one Set of MyPerson Entities. – Jason Sep 29 '20 at 12:20

2 Answers2

0

data=Collection.stream().filter(f->f.getName().equals("name")).forEach(i->{
    System.out.println();
});
GuoZG
  • 1
0

I found a solution:

final CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<MyPersons> query = builder.createQuery(MyPersons.class);
Root<MyPersons> root = query.from(MyPersons.class);
Join<MyPersons, MyNaturalPerson> join = root.join("persons");
ParameterExpression<Collection> persons = builder.parameter(Collection.class);
//Persons = filled with MyPerson Entities
query.where(join.in(persons));
TypedQuery<MyPersons> tq = session.createQuery(query);
List<?> resultList = tq.setParameter(persons, result).getResultList();
List<MyNaturalPerson> finalresult = (List<MyNaturalPerson>) resultList;

Thx to a very similar Question/problem

Jason
  • 1