Smart_it
Smart_it

Reputation: 59

How to select an attribute of a list elements in jpql

I have a Project entity, containing a list of its creators,

public class Project implements Serializable { @ManyToMany private List<Creator> creator;

I need a method with a parameter creator id, returning a list of the projects created by that creator, here is my idea

 public List<Project> MyOwnProjects(int idcreator){
String jpql= "select p from Project p where p.creator.get(i).getCode()= :idcreator";

Query query= em.createQuery(jpql);
query.setParameter("idcreator", idcreator);     
return (List<Project>)query.getResultList();
} 

This query is not valid of course, How can I do that ?

Upvotes: 1

Views: 1334

Answers (1)

HBo
HBo

Reputation: 633

Unless you can't modify the mapping, I would do it the other way around : add a @ManyToMany association in your Creator entity (using MappedBy), then something like this (my JPQL is quite rusty)

"select p from creator left join fetch creator.project p where creator.id = :idcreator "

Upvotes: 1

Related Questions