Ar-jay Alaon
Ar-jay Alaon

Reputation: 55

How to fetch data from joined tables straight to a class?

Is it possible to retrieve data from a query straight into the EmployeeForm?

Query as stored procedure empdata

SELECT a.name,b.username,b.password FROM Tbemployee left join Tbuser

Code

   List<EmployeeForm> form = new ArrayList<EmployeeForm>();
    EmpDB service = (EmpDB) RuntimeAccess.getInstance().getServiceBean(
    service.begin();
    Session session = service.getDataServiceManager().getSession();
    SQLQuery query = session.createSQLQuery("EXEC empdata");
    List list = query.list();
    formList = list;

This gives me an error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.emp.form.EmployeeForm

Upvotes: 3

Views: 95

Answers (1)

isah
isah

Reputation: 5341

You need to use a ResultTransformer

The other option is to cast to List<Object[]> which contains rows with columns from the query result, and then iterate and extract data(more work).

The transformer could be something like:

query.setResultTransformer(Transformers.aliasToBean(EmployeeForm.class));

Upvotes: 1

Related Questions