命名查询未知:byShipmentNumber

时间:2013-03-19 15:23:28

标签: spring hibernate

似乎无法弄清楚为什么它看不到我的命名查询。希望有人可以对此有所了解:
这是代码:

@Data
@Entity
@Table(name = "shipments")
    @NamedNativeQueries({
        @NamedNativeQuery(name = "byShipmentNumber", query = "select S.SHIPMENTS_ID,S.SHIPMENT_NUMBER,S.COMPANIES_ID, S.COMPANIES_ID_3,(min(sil.IMPORT_PCT)) MIN_PCT,(max(sil.IMPORT_PCT)) MAX_PCT,"
        + "(case when ((nvl((SUM(case when (nvl(SIL.IMPORT_PCT,0)>0) then 1 end)),0)) < count(nvl(SIL.IMPORT_PCT,0))) then 0 else 1 end) Status from SHIPMENTS s "
        + "join shipment_invoices si on s.shipments_id = si.shipments_id"
        + "join SHIPMENT_INVOICE_LINES sil on SI.SHIPMENT_INVOICES_ID = sil.SHIPMENT_INVOICES_ID"
        + "where s.SHIPMENT_NUMBER = :shipmentNumber"
        + "and companies_id=23351763"
        + "group by S.SHIPMENTS_ID,S.SHIPMENT_NUMBER,S.COMPANIES_ID, S.COMPANIES_ID_3;",
        resultClass = Shipments.class)
    })
    public class Shipments implements Serializable {


        @Id
        @Column(name = "shipments_id")
        private Long id;
        @Column(name = "shipment_number")
        private String shipmentNumber;
        @Column(name = "companies_id")
        private String billTo;
        @Column(name = "companies_id_3")
        private String issuer;
        @Column(name = "status")
        private String status;
        @Column(name = "min_pct")
        private String minImportCost;
        @Column(name = "max_pct")
        private String maxImportCost;


        public static List<Shipments> getShipmentByShipmentNumber(Session session, String shipmentNumber) {
            Query query = session.getNamedQuery("byShipmentNumber");
            query = query.setString("shipmentNumber", shipmentNumber);
            return query.list();
        }

这是错误:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ImportCost.spring] threw exception [Request processing failed; nested exception is org.hibernate.MappingException: Named query not known: byShipmentNumber] with root cause
org.hibernate.MappingException: Named query not known: byShipmentNumber

2 个答案:

答案 0 :(得分:0)

我认为这是因为您的班级没有注释为@Entity,因此JPA会忽略它。

答案 1 :(得分:0)

是设置错误,而不是编程错误。
在我的db.xml(春天)我正在扫描错误的文件夹。

bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="packagesToScan" value="**dk.lector.bestinfoII.importcost.domain**" 
....(other properties)....
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:component-scan base-package="**dk.lector.bestinfoII.importcost.domain**"/>

**我链接到错误文件夹的地方