Upon trying to retrieve a table from database through JPA and fill it into the Table SWT widget (org.eclipse.swt.widgets.Table
), I get a cast error while executing the following Java code:
EntityManager connection = DB.Connection().createEntityManager(); //the EntityManagerFactory
TypedQuery<Object[]> query = (TypedQuery<Object[]>) connection.createQuery("select t from Test t");
List<Object[]> tablelist = query.getResultList();
connection.close();
SWTtable.removeAll(); //clears all the elements in the table
for (Object[] row : tablelist) { //error on this line - java.lang.ClassCastException: {packagename}.Test cannot be cast to [Ljava.lang.Object;
final TableItem item = new TableItem(SWTtable, SWT.None);
item.setData(row[0]);
item.setText(0, row[1].toString());
}
As seen above, this line for (Object[] row : tablelist) {
returns an error of java.lang.ClassCastException: {packagename}.Test cannot be cast to [Ljava.lang.Object;
It seems that there is an error casting tablelist
which is List<Object[]>
to Object[] row
in the foreach loop. I have tried various solutions, but cannot figure this out as I need to retain the option of Object[]
array in order to have all the row elements at my disposal as items for the SWT table. Everything else works fine except this line of code. If it's set to for (Object row : tablelist)
, so no array. Then there is no error, but I lose the array and thus lose the option to have all the elements at my disposal as items for the SWT table.
If this is a bad approach and you have other suggestions, please specify.
Note: The SQL query, "select t from Test t" is just testing code. I will use a parmeter to specify which records I need later, so there will be more then one table here.