How can I make a named query with IN actually work?

class X {
Y y; // manyToOne
}
class Y {
Long id;
}


@NamedQuery(name = "someName", query = "from X where y.id in :ids")

I have public, table, entity and everything else on Entities, but I did not write them here.

TypedQuery<X> query = getEntityManager().createNamedQuery("someName", X.class);
query.setParameter("ids", someListOfLongs); // HERE I GET THE ERROR
queryFinal.getResultList();


Parameter value [[Ljava.lang.Object;@90d0bf] was not matching type [java.lang.Long]

I tried with or without (), I changed the version of Hibernate-Core to 3.6.4 (from JBoss 6.0.0.Final), otherwise, if I wrote in :idswithout (), I got an error.

Please, help.


The IN always worked, the problem was that List<Long> wasn't actually List<Long> was List<Object[]>. Thanks

+3
source share
2 answers

I also use JBoss AS 6 and this exact design, but it just works.

This is an example request:

<named-query name="Item.getByItemIDs">
    <query>
        SELECT
            i
        FROM
            Item i
        WHERE
            i.ID in (:itemsIDs)
    </query>
</named-query>

And the class using it:

@Override
public List<Item> getByItemIDs(List<Long> itemIDs) {
    return entityManager.createNamedQuery("Item.getByItemIDs", Item.class)
                        .setParameter("itemIDs", itemIDs)
                        .getResultList();
}

As indicated in your exception [[Ljava.lang.Object;@90d0bf](which is the object []), perhaps you should try List <Long>, as in my example?

(p.s. API- JPA, )

+4

"IN" , , , , , .

@NamedQuery(name = "someName", query = "select x from X x where x.y.id in (?)")

,

TypedQuery<X> query = getEntityManager().createNamedQuery("someName", X.class);
query.setParameter(1, someListOfLongs); // I can't remember if the position is 0 or 1 based.
queryFinal.getResultList();

, emtpy, . , "-1", , .

+1

All Articles