Search in associative table

3

How can I search the associative table? There are two entities, Person and Time. From where I created the associative Person_Time that has id_people and id_time.

My goal is to list all the id_time of a certain people_id

Next error:

HTTP Status 500 - org.hibernate.hql.ast.QuerySyntaxException: persona_time is not mapped [select t from persona_time u where u.id_pessoa =: pPeople]

PersonaDAO:

public List<Time> listarmeustimes(Pessoa pessoa) {

        Pessoa resultado = new Pessoa();

        String consulta = "select t from pessoa_time u where u.id_pessoa = :pPessoa";
        Query query = getEm().createQuery(consulta);

        query.setParameter("pPessoa", pessoa.getNomeUsuario());

        List<Time> meustimes = query.getResultList();
        for (Time time : meustimes) {
            System.out.println(time.getNome());

        }
        return meustimes;
    }

PersonaBean:

public void listarmeustimes(){
        getDao().listarmeustimes(getPessoa());  
    }

Person Model:

@ManyToMany(mappedBy="listaPessoas")
private List<Time> listaTimes; 

Time Model:

@ManyToMany
 @JoinTable(name="Pessoa_Time",joinColumns={@JoinColumn(name="id_time")}, 
 inverseJoinColumns={@JoinColumn(name="id_pessoa")})
private List<Pessoa> listaPessoas;
    
asked by anonymous 12.07.2016 / 16:18

1 answer

3

There is an error in your query.

Notice here where u.id_pessoa = :pPessoa" . The query expects to receive the Person parameter, but you are passing is the name here :pPessoa

The right should be query.setParameter("pPessoa", pessoa.getNomeUsuario());

But since what you really want is to list all the teams that are related to a person, then change your query to be like this query.setParameter("pPessoa", pessoa.getId());

And in%% pass id instead of name , like this: String consulta = "from Time t join t.pessoas p where p.id = :pPessoa";

    
12.07.2016 / 18:03