0

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.

5
  • 1
    What error is thrown from which line? Commented Feb 10, 2018 at 1:06
  • Mostly the issue is you are returning something from the query that is not likely possible to cast into List<Object[]> Commented Feb 10, 2018 at 6:49
  • And what error it gives when you return List<TBatchEntry> ? It should work if you map your Entity properly Commented Feb 10, 2018 at 6:54
  • Previously I was doing HQL query. Then List<TBatchEntry> batchListFromQuery= new ArrayList<TBatchEntry>(); was working good. When I tried with SQL query then causing problem. in mappins is same. I have added my entity class. Please see that. Commented Feb 10, 2018 at 7:06
  • After reading some related issue in this site, I changed my Arraylist declaration to List<Object[]> batchListFromQuery = new ArrayList<Object[]>(); Commented Feb 10, 2018 at 7:10

3 Answers 3

1

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());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

After watching a video on SQL query of Hibernate, I got my problem. I didn't add '.addEntity()' after query. Thanks for your answer. It gave me surity that the problem was not adding '.addEntity()'.
@Sourov, If you got your solution.You can accept my answer.
0

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;
}

5 Comments

Thanks for your answer. Actually directy catch into TBatchEntry class throws same error. I have tried this at first. Then reading some similar article from stackoverflow I changed it to Object[].
I tried the same thing into my project and it is working fine for me, it is not throwing any type of exceptions.
Dont use iterator , use foreach loop for displaying name.
I have also tried with foreach loop. Are you using SQL query ?
use addEntity(TBatchEntry.class)
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.