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.
List<Object[]>List<TBatchEntry>? It should work if you map your Entity properly