简体   繁体   English

休眠:将HQL查询切换为SQL查询,并引发异常:java.lang.ClassCastException

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

In My DaoImpl class I am trying to fetch list of data of Type TBatchEntry(model class) 在我的DaoImpl类中,我试图获取TBatchEntry类型的数据列表(模型类)

@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: 在我的Controller类中,我尝试打印值,但是它在注释行中引发错误:

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 Tomcat根源日志

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. 如果有人可以帮助我,我将非常感激。

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

Why you are catching TBatchEntry into Object class.You can directly catch into TBatchEntry class. 为什么将TBatchEntry捕获到Object类。您可以直接捕获到TBatchEntry类。 Change Object[] into TBatchEntry Class, because you are selecting all columns from TBatchEntry table right, try below code i think it will work, 将Object []更改为TBatchEntry类,因为您正在从TBatchEntry表中选择所有列,请尝试以下代码,我认为它将起作用,

1) From Controller, 1)从控制器,

List batchListFromQuery = new ArrayList<>(); 列表batchListFromQuery = new ArrayList <>(); use foreach loop for displaying records 使用foreach循环显示记录

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

After some study I understood the difference between HQL & SQL query in hibernate. 经过一些研究后,我了解了HQL和Hibernate中的SQL查询之间的区别。

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

In case of using HQL query: 如果使用HQL查询:

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

In case of using SQL query: 如果使用SQL查询:

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

Difference is: 区别是:

.addEntity(TBatchEntry.class) .addEntity(TBatchEntry.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM