java.lang.IllegalArgumentException:未使用Hibernate 5为该名称定义查询

时间:2019-01-09 17:56:53

标签: java hibernate hibernate-mapping

我正在将代码从休眠3迁移到休眠5.3.7。 我正面临以下问题。 相同的代码可以在Hibenrate 3上正常工作。 请建议需要进行哪些更改才能在hibernate 5中识别命名查询。

即使我不能插入/更新或删除记录。因为会话无法识别我的模型类。

java.lang.IllegalArgumentException: No query defined for that name [findFactLoadByPrimaryKey]
        at org.hibernate.internal.AbstractSharedSessionContract.getNamedQuery(AbstractSharedSessionContract.java:620)
        at org.hibernate.internal.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:23)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

Kindly help what is the issue.
This entire code working fine on Hibernate 3.


my hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- properties -->
        <property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>
        <property name="show_sql">false</property>
        <property name="hibernate.transaction.coordinator_class">jdbc</property>
        <property name="current_session_context_class">thread</property>
        <property name="jdbc.batch_size">25</property>
        <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>

        <!-- Mapping resource files -->
        <mapping resource="com/sample/data/CapFact.hbm.xml"/>
        <mapping resource="com/sample/data/CapFactDetails.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

CapFact.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class
        name="com.sample.data.CapFact"
        table="CAP_FACT"
        lazy="false"
    >

        <id
            name="loadId"
            column="LOAD_ID"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="seqhilo">
                <param name="sequence">CAP_FACT_SEQ</param>
                <param name="max_lo">5</param>
              <!--  
                  To add non XDoclet generator parameters, create a file named 
                  hibernate-generator-params-CapFact.xml 
                  containing the additional parameters and place it in your merge dir. 
              --> 
            </generator>
        </id>

        <set
            name="details"
            lazy="true"
            cascade="all"
            sort="unsorted"
            order-by="LOAD_ID"
        >

            <key
                column="LOAD_ID"
            >
            </key>

            <one-to-many
                  class="com.sample.data.CapFact"
            />

        </set>

        <property
            name="factType"
            type="java.lang.Long"
            update="true"
            insert="true"
            column="FACT_TYPE"
        />

        <property
            name="status"
            type="java.lang.Long"
            update="true"
            insert="true"
            column="STATUS"
        />

        <property
            name="startTime"
            type="java.util.Date"
            update="true"
            insert="true"
            column="START_TIME"
        />

        <property
            name="endTime"
            type="java.util.Date"
            update="true"
            insert="true"
            column="END_TIME"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-CapFact.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

        <query name="findFactLoadByPrimaryKey"><![CDATA[
            from com.sample.data.CapFact as cfl where cfl.loadId = :load_id
        ]]></query>

</hibernate-mapping>

CapFact class:

package com.sample.data;
import javax.persistence.Entity;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

/**
 * This class is the Hibernate data object for CapFact
 * 
 * @hibernate.class 
 *   table="CAP_FACT"
 *   lazy="false"
 * @hibernate.query
 * name="findFactLoadByPrimaryKey"
 * query="from com.sample.data.CapFact as cfl where cfl.loadId = :load_id"
 *   
 */
public class CapFact implements Serializable{
    private static final long serialVersionUID = 5292146533123932568L;
    protected Long loadId;
    protected Date startTime;
    protected Date endTime;
    protected Long status;    
    protected Long factType;




    /**
     * @return Returns the loadId.
     * 
     * @hibernate.id
     *   column="LOAD_ID"
     *   generator-class="seqhilo"
     *   unsaved-value="null"
     *
     *   @hibernate.generator-param 
     *      name="sequence" value="CAP_FACT_SEQ"
     *   @hibernate.generator-param 
     *      name="max_lo" value="5"
     */
    public Long getLoadId() {
        return loadId;
    }

    /**
     * @param loadId The loadId to set.
     */
    public void setLoadId(Long loadId) {
        this.loadId = loadId;
    }

    /**
     * @return Returns the tasks.
     * 
     * @hibernate.set 
     *  cascade="all"
     *  lazy="true"
     *  order-by="LOAD_ID"
     *  
     * @hibernate.collection-key
     *  column="LOAD_ID"
     *  
     * @hibernate.collection-one-to-many
     *  class="com.sample.data.CapFactDetails"
     */
    @SuppressWarnings("unchecked")
    public Set getDetails() {
        return details;
    }


    /**
     * @param tasks The details to set.
     */
    @SuppressWarnings("unchecked")
    public void setDetails(Set details) {
        this.details = details;
    }

    /**
     * @return Returns the Fact Type.
     * 
     * @hibernate.property
     *  column="FACT_TYPE"
     *  
     */
    public Long getFactType() {
        return factType;
    }

    /**
     * @param The FACT_TYPE to set.
     */
    public void setFactType(Long factType) {
        this.factType = factType;
    }


    /**
     * @return Returns the status.
     * 
     * @hibernate.property
     *  column="STATUS"
     *  
     */
    public Long getStatus() {
        return status;
    }


    /**
     * @param status The status to set.
     */
    public void setStatus(Long status) {
        this.status = status;
    }

    /**
     * @return Returns the startTime.
     * 
     * @hibernate.property
     *  column="START_TIME"
     *  
     */
    public Date getStartTime() {
        return startTime;
    }

    /**
     *  
     * @param startTime The startTime to set.
     */
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    /**
     * @return Returns the endTime.
     * 
     * @hibernate.property
     *  column="END_TIME"
     *  
     */
    public Date getEndTime() {
        return endTime;
    }

    /**
     *  
     * @param endTime The endTime to set.
     */
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    } 
}

this piece of code throwing error:
List loadslist = session.getNamedQuery("findFactLoadByPrimaryKey").setLong("load_id", loadId).list();

in below code:

public String getCapFactType(long loadId)
    throws HibernateException {
        String factType = null;

        Session session = null;
        try {
            SessionFactory sf = getSessionFactory();
            session = sf.openSession();
            List loadslist = session.getNamedQuery("findFactLoadByPrimaryKey").setLong("load_id", loadId).list();

            if (loadslist!=null && loadslist.size() == 1) {
                CapFact load = (CapFact) loadslist.get(0);

                List typeList = session.getNamedQuery("findFactLoadByPrimaryKey")
                            .setLong("type_id", load.getFactType()).list();

                if (typeList.size() == 1) {
                    CapFactTypes type = (CapFactTypes) typeList.get(0);
                    factType = type.getFactType();
                }
            }
        }catch (HibernateException he) {
            LOGGER.error("exception at getCapFact" + he.getMessage());
        }
        finally {
            CapHibernateUtils.closeQuietly(session);
        }
        return factType;
    }

expected value is a string

0 个答案:

没有答案