Hibernate框架Session.createQuery方法抛出java.lang.NoSuchMethodError异常

发布于 2022-09-06 08:51:44 字数 7761 浏览 21 评论 0

用Maven做SSH框架整合模拟登陆页面时,创建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)

希望大神们能帮助小弟。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2022-09-13 08:51:44

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>

缱倦旧时光 2022-09-13 08:51:44

尝试去掉hibernate-entitymanager依赖,只用hibernate-core,看看可否?

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>5.2.12.Final</version>
</dependency>
淡墨 2022-09-13 08:51:44

请问是如何解决的,求教

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文