【问题标题】:org.hibernate.MappingException Named query not knownorg.hibernate.MappingException 命名查询未知
【发布时间】:2017-09-18 08:10:23
【问题描述】:

我有以下实体。当我使用命名查询时,我给出 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();

我该如何解决?

【问题讨论】:

  • 添加使用位置的相关代码并获取异常
  • 请添加 hibernate.cfg.xml 或类似配置。 Hibernate 知道实体在哪里吗?

标签: hibernate jpa named-query


【解决方案1】:

我忘记在休眠配置中添加ServiceDataTO。

【讨论】:

    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多