Database Query with an Array in Hibernate

Asked

Viewed 86 times

0

I have a method that takes as parameter an array of values(id) and I need to do a query in the database to return only the rows of a column with these values(id) I have this method:

public List<Lotacao> localizarPorId(long[] id){
    try {

        String sql = "select l from Lotacao l where l.id in :id";
        TypedQuery<Lotacao> q = em.createQuery(sql, Lotacao.class);

        for( int i = 0; i < id.length; i++) {
                q.setParameter("id", id[i]);
            }
        return q.getResultList();

        }catch(NoResultException e) {
            return new ArrayList<>();
        }
    }

However, it only returns from the database the line that corresponds to the value of the last index of the Array. Does anyone know how to get it to return all the corresponding BD lines in the Array values?

1 answer

0

You adapt to your, :D

List<Lotacao> logins = em.createQuery("select l from Lotacao l where l.id in :ids")
        .setParameter("ids",Arrays.asList(new Long(1000100), new Long(1000110)))
        .getResultList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.