I'm new to Hibernate, and trying to use it in a Spring MVC application. I have a custom query like the following:
public List<MyResults> findEmployees() {
String queryString = "select E.firstname, E.lastname, A.city, A.state
from Employee E, Address A, where ...."; // whatever where clause
SQLQuery query = this.sessionFactory.getCurrentSession().createSQLQuery(queryString);
query.setString(...) // set query parameters
return query.list();
}
This doesn't work. The query runs, but it returns a list of objects.
How do I tell Hibernate to use my MyResults
class?
class MyResults {
private String firstName;
// other fields in the search
@Column(name = "FirstName")
String getFirstName() {
return firstName;
}
void setFirstName(String firstName) {
this firstName = firstName;
}
// other getters & setters
}
I'm also not sure what annotations MyResults
needs?
I've tried a couple things:
return (List<MyResults>)query.list();
though this cast doesn't do it.
I've tried using a ResultTransformer:
query.setResultTransformer(new AliasToBeanResultTransformer(MyResults.class)); //done before the return
but that causes an
ClassClastException: MyResults cannot be cast to java.util.Map.
Can someone show me how to set this kind of query up correctly in Hibernate?