Search code examples
hibernatejakarta-eejpajpql

join statement using JPQL (using hibernate jpa) to store in a list


I have a worklog table which have onetomany join to Jiraissue table which also have ManyToOne to several other tables.

I wrote a jqpl statement as below based on my requirements.

@NamedQueries({
    @NamedQuery(name = "Worklog.findbySelection", query = "SELECT distinct workl.jiraissueWorklog.pkey, workl.jiraissueWorklog.summary, workl.jiraissueWorklog.issuestatusOBJ.pname, workl.jiraissueWorklog.cwduserOBJ.displayName, workl.jiraissueWorklog.created, workl.jiraissueWorklog.updated   FROM Worklog workl where workl.jiraissueWorklog.project = :project  and  workl.startdate between :startDate AND  :endDate" ),
     })

Now i want to store the output in a list object, so i gave it as Worklog object.

    @SuppressWarnings("unchecked")
    public List<JiraissueColumns> findByDateProject(Date startDate,Date endDate,long projectID){

        Query query = em.createNamedQuery("Worklog.findbySelection");
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);
        query.setParameter("project", projectID);
        this.listOfWorklog = (List<JiraissueColumns>) query.getResultList();
            return listOfWorklog;
    }

When I use for loop to find what is there in the list, I get the following error.

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to listOfWorklog

I did understand that listOfWorklog can only accept of type Worklog and Jiraissue objects, but Jiraissue in turn have Project, issuelist object. Would those get inherited to Worklog.

What is the right way of getting output from the JPQL statement?


Solution

  • You are using non-generic Query class. For parametrized type of Query there is a TypedQuery:

    TypedQuery<JiraissueColumns> query = em.createNamedQuery(
        "Worklog.findbySelection",
        JiraissueColumns.class);
    
    query.setParameter("startDate", startDate);
    query.setParameter("endDate", endDate);
    query.setParameter("project", projectID);
    
    return listOfWorklog = query.getResultList();
    

    this will return List<JiraissueColumns> by calling query.getResultList().