EJB ClassCastException

  • Problem:
    [Ljava.lang.Object; cannot be cast to x.y.z.beanEJB: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to x.y.z.beanEJB
  • Solution:
    Add a “SELECT” statement to your (named) query.
    e.g. change :

    FROM beanEJB e
    INNER JOIN e.tags AS t
    WHERE t LIKE :ttt

    to

    SELECT e FROM beanEJB e
    INNER JOIN e.tags AS t
    WHERE t.string LIKE :ttt

  • Further information:
    A (named) query may return more than one distinct Object. An additional SELECT clause will specify the return type.
  • Code snippets:
    • </named-query>
      <named-query name=”namedQuery”>
      <query>
      <![CDATA[SELECT e FROM beanEJB e
      INNER JOIN e.tags AS t
      WHERE t LIKE :ttt]]>
      </query>
      </named-query>

    • Query q = em.createNamedQuery(“namedQuery”);
      q.setParameter(“ttt”, s);
      Log.info(q.getResultList());