JPA:如何从EntityManager返回泛型/类型类?

时间:2014-07-28 14:41:40

标签: generics jpa entity named-query

如何让Eclipse理解我期望em.createQuery(...)的类型结果?

public class ReservationDaoImpl<T extends SessionInfoEntity<T>> extends JpaDaoImpl<ReservationEntity<T>> 
    implements ReservationDao<T> {
public ReservationEntity<T> findById(final Long reservationId) {
        em = factory.createEntityManager();
        final TypedQuery<ReservationEntity<T>> resQuery =
                em.createNamedQuery("ReservationEntity.findById", ReservationEntity.class) 
                .setParameter("reservationId", reservationId)
                .setMaxResults(1);
            ReservationEntity<T> resE = null;
            try {
                resE = resQuery.getSingleResult();
            } catch( NoResultException nrEx) {
                //...
            }

            em.close();

            return resE;
    }
}

Eclipse抱怨: 类型不匹配:无法转换为TypedQuery&lt; ReservationEntity&gt;调用时键入查询&lt; ReservationEntity&lt; T&gt;&gt;

final TypedQuery<ReservationEntity<T>> resQuery = 
    em.createNamedQuery("ReservationEntity.findById", ReservationEntity.class) 
                .setParameter("reservationId", reservationId)
                .setMaxResults(1);

0 个答案:

没有答案