鍍金池/ 問答/Java  網絡安全/ Hibernate框架Session.createQuery方法拋出java.l

Hibernate框架Session.createQuery方法拋出java.lang.NoSuchMethodError異常

用Maven做SSH框架整合模擬登陸頁面時,創(chuàng)建Session.createQuery()方法拋出java.lang.NoSuchMethodError異常,查閱相關資料,說是Query類已從org.hibernate轉移到org.hibernate.query里面,但是我就是用的hibernate5的org.hibernate.query.Query類。
以下是dao層的代碼

package dao;

import entity.UserManageEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;

public class UserManageDao  {

    private String uname;
    private String upassword;
    private SessionFactory sessionFactory=null;
    private Session session;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public String userManageJudge(UserManageEntity userManageEntity) {
        this.uname=userManageEntity.getUname();
        this.upassword=userManageEntity.getUpassword();
        session=sessionFactory.openSession();
        String hql="from UserManageEntity where uname=? and upassword=?";
        Query query= session.createQuery(hql); //這里報錯
        query.setString(0,uname);
        query.setString(1,upassword);
        if(query.list().size()>0){
            return "success";
        }else {
            return "error";
        }
    }


}

這是pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MyBlog</groupId>
    <artifactId>My</artifactId>
    <version>1.0-SNAPSHOT</version>

    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- struts2-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <!-- mysql lib-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.24</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.7.Final</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>ssh</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 是否替換資源中的屬性-->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>

                <!-- 是否替換資源中的屬性-->
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

以下是具體異常信息

21-Dec-2017 22:48:23.255 嚴重 [http-nio-8080-exec-4] org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler.error Exception occurred during processing request: Method "userManageJudge" failed for object action.UserManageAction@6530d49b
 ognl.MethodFailedException: Method "userManageJudge" failed for object action.UserManageAction@6530d49b [java.lang.NoSuchMethodError: org.hibernate.Session.createQuery(Ljava/lang/String;)Lorg/hibernate/query/Query;]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1305)
    at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
    at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
    at ognl.ASTMethod.getValueBody(ASTMethod.java:90)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at ognl.SimpleNode.getValue(SimpleNode.java:258)
    at ognl.Ognl.getValue(Ognl.java:494)
    at ognl.Ognl.getValue(Ognl.java:458)
    at com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309)

還有這一段

/-- Encapsulated exception ------------\
java.lang.NoSuchMethodError: org.hibernate.Session.createQuery(Ljava/lang/String;)Lorg/hibernate/query/Query;
    at dao.UserManageDao.userManageJudge(UserManageDao.java:28)
    at service.UserManageService.userManageJudge(UserManageService.java:15)
    at action.UserManageAction.userManageJudge(UserManageAction.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
    at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
    at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
    at ognl.ASTMethod.getValueBody(ASTMethod.java:90)

希望大神們能幫助小弟。

回答
編輯回答
憶往昔

Hibernate-core換這個版本試試,你的5.0.7也是沒有org.hibernate.query.Query這個接口的
<dependency>

<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>

</dependency>

2017年6月6日 06:18
編輯回答
孤星

嘗試去掉hibernate-entitymanager依賴,只用hibernate-core,看看可否?

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>5.2.12.Final</version>
</dependency>
2017年7月5日 03:49
編輯回答
愚念

請問是如何解決的,求教

2017年7月22日 07:03