1 Reply Latest reply: Oct 17, 2013 8:39 AM by Cdelahun-Oracle RSS

    Join in JPA

    Prat04

      Following is the code in which I am not able to solve the issue as per requirements i want apply join and display result of EMPLOYEES and DEPARTMENTS table of oracle hr

      Kindly find the code and reply ASAP

       

       

      try {

                  EntityManagerFactory emf = Persistence.createEntityManagerFactory("CriteriaBuilderCompPU");

                  EntityManager em = emf.createEntityManager();

                  CriteriaBuilder cb = em.getCriteriaBuilder();

                  CriteriaQuery cq = cb.createQuery();

                  Root<Employees> e = cq.from(Employees.class);

       

                 

                  Join d = e.join("departments",JoinType.LEFT);

                  cq.multiselect(e.get("firstName"),d.get("departmentId"));

                  TypedQuery<Tuple> tq = em.createQuery(cq);

                  List<Tuple> t = tq.getResultList();

                  for (Tuple result : t) {

                      String fn = result.get(0, String.class);

                      String ln = result.get(1, String.class);

                      System.out.println(fn + "\t" + ln);

                  }

                 

                 

                  em.close();

                  emf.close();

              } catch (Exception e) {

                  System.out.println("Exception   " + e.getMessage());

              }

          }


      -----------------------------

      Exception:

      Exception   [Ljava.lang.Object; cannot be cast to javax.persistence.Tuple