0

I am firing the HQL Join query

@Override
public List<Object> getCourseIdWithStatus(int courseId, String status) {

    String queryString = "select sc.courseId, sc.name, sct.streamId from Course "
            + "sc join CoursesTypeInformation sct ON(sc.courseId = sct.courseId)"
            + " where sc.courseId = ?1 and sc.status = ?2 and sct.status = ?3";
    Query query = entityManager.createQuery(queryString);
    query.setParameter(1, courseId);
    query.setParameter(2, status);
    query.setParameter(3, status);
    return (List<Object>)query.getResultList();
}

This is a HQL Query not a native SQL

Now I have a pojo class

public class BasicData {

    private int courseId;

    private String name;

    private int streamId;

    .. setters and getters

}

I want to map result of query to POJO. WHat should I use?

Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79
  • Possible duplicate of [mapping Hibernate query results to custom class?](https://stackoverflow.com/questions/37420401/mapping-hibernate-query-results-to-custom-class) – Vlad Bochenin Aug 24 '17 at 11:41
  • The solution is defined for Native SQL not HQL. – Ankit Bansal Aug 24 '17 at 11:44
  • I think you can directly populate your custom object if you are using HQL. Try this: (List)query.getResultList(); – Afridi Aug 24 '17 at 11:48
  • tried it, not working java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.domain.customDTO.BasicData – Ankit Bansal Aug 24 '17 at 11:49

1 Answers1

0

there is many ways to achieve this, one of it by creating constructor in your POJO class here some example taken from my code :

POJO :

@Entity
@Table(name="TBL_KCT_SPL_STOCK_DTL")
public class TblKctSplStockDtl implements Serializable{
   ...
   public TblKctSplStockDtl(String reclaim, BigDecimal){ 
    this.reclaim = reclaim;
    this.tonnage = tonnage;
   }
  ...
} 

HQL :

String hql = "select new TblKctSplStockDtl(t.reclaim, sum(t.tonnage)) from TblKctSplStockDtl t "
            + "where t.idStock = :idStock and t.state = 'IN' "
            + "group by t.reclaim ";

Query q = sessionFactory.getCurrentSession().createQuery(hql);
Gusti Arya
  • 1,281
  • 3
  • 15
  • 32