账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Hibernate框架Session.createQuery方法抛出java.lang.NoSuchMethodError异常
    40
    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)
    

    希望大神们能帮助小弟。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 琉离 普通会员 1楼

      在Hibernate框架中,Session.createQuery方法会抛出NoSuchMethodError异常,这是因为Hibernate不支持一些Java的接口方法,如Hibernate.initialize,Hibernate寮出等。这些方法是由Hibernate自己实现的,而Hibernate的Session接口中并没有这些方法。

      为了解决这个问题,你需要确保你使用的Hibernate版本支持这些方法。例如,如果你使用的是Hibernate 5.0或更高版本,那么你不需要担心这个问题,因为Hibernate已经实现了这些方法。

      如果你使用的是Hibernate 4.3或更低版本,那么你需要确保你安装了Hibernate 4.3以上的版本。你可以通过在你的项目中添加Hibernate的依赖来解决这个问题。

      另外,你也可以使用Hibernate的JPA API来实现你的查询需求。JPA API提供了更简单的方式来实现Hibernate的查询需求。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部