Hibernate: Switched HQL query to SQL query, throws exception: java.lang.ClassCastException

600 Views Asked by At

In My DaoImpl class I am trying to fetch list of data of Type TBatchEntry(model class)

@Override
public List<TBatchEntry> getBatchListFormQuery(String batchNo) {

    session = sessionFactory.openSession(); 
    List<TBatchEntry> batchListFromQuery = new ArrayList<TBatchEntry>();
    try {           
        tx = session.beginTransaction();
        batchListFromQuery =  session.createSQLQuery("SELECT * FROM pghms.t_batchentry WHERE t_regNo LIKE '2008%'").list(); 
        tx .commit();           
    }catch(Exception e) {       
        e.printStackTrace();
        session.getTransaction().rollback();            
    }
    return batchListFromQuery;
}

In my Controller class I am trying to print value but it is throwing error in commented line:

List<TBatchEntry> batchListFromQuery = new ArrayList<TBatchEntry>();

try{
        batchListFromQuery = adminService.getBatchListFormQuery(batchNo);
 }catch(Exception e){
        e.printStackTrace();
 }
Iterator its = batchListFromQuery.iterator();
    while(its.hasNext()){
        batchFromQuery = (TBatchEntry) its.next();  //This line thorws error
        System.out.println(batchFromQuery.getName());
    }

This is my entity class

@Entity
@Table(name="t_batchEntry")
public class TBatchEntry {

  @Id
  @Column(name="t_regNo")
  private String regNo;
  @Column(name="t_name")
  private String name;

  public String getRegNo() {
    return regNo;
  }
  public void setRegNo(String regNo) {
    this.regNo = regNo;
  }
 public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

}

log of tomcat`root cause

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sv.pghms.model.TBatchEntry 

I'd be really thankful, if somebody could help me.

3

There are 3 best solutions below

2
On BEST ANSWER

Try this way just change class name and where condition.It is working for me. Hope so it will work for you.

List<Book> books = this.sf.getCurrentSession().createSQLQuery("select * from Book where book_id > 3")
                         .addEntity(Book.class)
                          .list();
    for (Book book : books) {
        System.out.println("Book Names are :: " + book.getBookName());
    }
5
On

Why you are catching TBatchEntry into Object class.You can directly catch into TBatchEntry class. Change Object[] into TBatchEntry Class, because you are selecting all columns from TBatchEntry table right, try below code i think it will work,

1) From Controller,

List batchListFromQuery = new ArrayList<>(); use foreach loop for displaying records

change return type as below :

    @Override
public List<TBatchEntry> getBatchListFormQuery(String batchNo) {
  session = sessionFactory.openSession(); 
    List<TBatchEntry> batchListFromQuery = new ArrayList<>();
    try {           
        tx = session.beginTransaction();
        batchListFromQuery =  session.createSQLQuery("SELECT * FROM pghms.t_batchentry WHERE t_regNo LIKE '2008%'").list(); 
        tx .commit();           
    }catch(Exception e) {       
        e.printStackTrace();
        session.getTransaction().rollback();            
    }
    return batchListFromQuery;
}
0
On

After some study I understood the difference between HQL & SQL query in hibernate.

List<TBatchEntry> batchListFromQuery = new ArrayList<TBatchEntry>();

In case of using HQL query:

batchListFromQuery = session.createQuery(sql).list()

In case of using SQL query:

batchListFromQuery = session.createSQLQuery(sql).addEntity(TBatchEntry.class).list();

Difference is:

.addEntity(TBatchEntry.class)