Hi,
I'm going to ask newbie questions so bear with me pls :)
I'm using JPA, Hibernate and DAO. I have the following classes
1) Car - database model
2) CarDAO - interface to CarDAOImpl
3) CarDAOImpl - All the SQL resides in here
I have the following method in my CarDAOImpl
Code:
@Transactional(propagation = Propagation.MANDATORY)
public List<Car> getAll() {
@SuppressWarnings("unchecked")
List<Car> result = getEntityManager().createQuery(
"SELECT r FROM Car r ORDER BY r.carModel").getResultList();
return result;
}
The HQL above queries everything from the Car table. When I execute this method, the getResultList() will return a list of Car objects which is cool.
But when I changed the HQL to the following, I didn't get the Car object anymore, instead result contains a list of Object[].
Code:
@Transactional(propagation = Propagation.MANDATORY)
public List<Car> getAll() {
@SuppressWarnings("unchecked")
List<Car> result = getEntityManager().createQuery(
"SELECT r.carModel, r.yearManufactured FROM Car r ORDER BY r.carModel").getResultList();
return result;
}
My question is how do I get a list of Car objects when I'm specifying columns to query from database? Is this not possible?
Any suggestions are welcome. Thanks in advance