org.hibernate.MappingException未知的命名查询

时间:2017-09-18 08:10:24

标签: hibernate jpa named-query

我有下面的实体。当我使用命名查询时,我给org.hibernate.MappingException:未知的命名查询:ServiceDataTO.findId。 为什么呢?

import javax.persistence.*;


@NamedQueries({
        @NamedQuery(
                name = ServiceDataTO.SELECT_ID,
                query = "select serviceId from ServiceDataTO sd where sd.serviceType = :serviceType"
        )}
)


@Entity
@Table(name = "MCI_SERVICE_DATA")
public class ServiceDataTO {

    public static final String SELECT_ID = "ServiceDataTO.findId";

    @Id
    @Column(name = "SDT_ID")
    private Long sdtId;

    @Column(name = "SDT_SERVICE_ID")
    private Long serviceId;

    @Column(name = "SDT_SERVICE_TYPE")
    private String serviceType;
}

客户代码:

   Session session = HibernateUtil.getSessionFactory().openSession();
   session.beginTransaction();
   List<Long> r = session.getNamedQuery(ServiceDataTO.SELECT_ID).setString("serviceType", serviceType).list();

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我忘了在hibernate配置中添加ServiceDataTO

相关问题