testing our jpa daos always worked successful… but i think this was mainly, because we filled during test the data on the database and got the data and tested it in the same transaction. therefore the data might be still stored in cache and never an actuall db call was made… this needs to be analysed and tested in near future… but thats a different blog entry… hopefully…
this blog entry is about the problem, which occures, when the db actuall is called 🙂
We have a GenericDao and all find all calls worked… and we have object based daos… following example, with an random object… lets take Person:
public List<Person> getPersonByName(final String name) {
@SuppressWarnings(“unchecked”)
final List<Person> list = (List<Person>) entityManager
.createNamedQuery(Person.GET_PERSON_BY_NAME)
.setParameter(“name”, name)
.getResultList();
return list;
}
This resulted in an exception… dont have the Exception here, but was something with an Object[] missmatch… and something about an non initialitzed EntityManager… the problem was the @SuppressWarnings(“unchecked”): or the warrning, which we just ignored… getResultList() returns a List… and casting List to List<Person> just doesnt work… whats the solution? Simple… just tell the named query, that kind of type it is… so it returns an List<Person> instead of the List…
.createNamedQuery(Person.GET_PERSON_BY_NAME, Person.class)
and the @SuppressWarnings(“unchecked”) isnt used anymore 🙂