Abstracting named queries in an abstract JPA DAO
hibernate, java, jpa, jpa-2.0
Solution
The naming convention for named queries is usually `<Entity Name>.findBy<PropertyAndAnotherProperty>`, "City.findByName" in your example, so I would try to change the named queries to follow this pattern. The parameter to this query should then also have the same name, or you could use positional parameters. Your find method would then turn into
@Override
public E findByName(String name) {
E entity = null;
try {
return (E)entityManager.createNamedQuery(myClass.getSimpleName() + ".findByName")
.setParameter("name", name)
.getSingleResult();
} catch (Exception ex) {
return null;
}
}
Problem
I have an abstract DAO class which uses parameterized types `E` (Entity) and `K` (Primary Key). In every entity I have a `@NamedQuery`. I want to dynamically invoke this named query without knowing its exact name and parameter name. As an example, imagine the following entity `City` ``` @Entity(name="CITY") @NamedQuery( name="findCityByname", query="FROM CITY c WHERE name = :CityName" ) public class City { // ... } ``` and this `CityDao` ``` public class CityDao extends AbstractDao<City, Long> { public CityDao() { super(City.class); } } ``` How should I implement the `findByName()` method in `AbstractDao` so that I don't need to know the exact name and parameter name? ``` public abstract class AbstractDao<E, K> implements Dao<E, K> { @PersistenceContext protected EntityManager entityManager; protected Class<E> entityClass; protected AbstractDao(Class<E> entityClass) { this.entityClass = entityClass; } @Override public E findByName(String name) { try { return (E) entityManager .createNamedQuery("findCityByName") .setParameter("CityName", name) .getSingleResult(); } catch(Exception e) { return null; } } // ... } ```