NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg / hibernate / classic / Session;

时间:2017-07-01 08:26:30

标签: java hibernate maven

我在Hibernate工作一段时间后遇到了一些困难,试图从我的数据库中访问数据。我搜索了很多错误并尝试了很多东西,只是不知道还有什么我不能做的,我尝试了关于我的pom依赖关系,但没有任何效果。这是:

主要课程:

 public class Main {
    private static SessionFactory factory; 

    public static void main(String[] args)
    {

//      User u = new User();
//      
//      UserDAO ud=new UserDAO(new Configuration().configure().buildSessionFactory());
//      ud.listUsers();


//           factory = ((AnnotationConfiguration) new AnnotationConfiguration().
//                     configure()).
//                     //addPackage("com.xyz") //add package if used.
//                     addAnnotatedClass(User.class).
//                     buildSessionFactory();
             factory = new Configuration().configure().buildSessionFactory();

        UserDAO ud=new UserDAO(factory);
        ud.listUsers();
    }
}

listUsers方法:

public void listUsers( ){
              Session session = factory.openSession();
              Transaction tx = null;
              try{
                 tx = session.beginTransaction();
                 List users = session.createQuery("FROM user").list(); 
                 for (Iterator iterator = 
                                   users.iterator(); iterator.hasNext();){
                    User employee = (User) iterator.next(); 
                    System.out.print("First Name: " + employee.getUsername()); 
                    System.out.print("Last Name: " + employee.getPassword()); 
                    System.out.println("Address: " + employee.getAddress()); 
                 }
                 tx.commit();
              }catch (HibernateException e) {
                 if (tx!=null) tx.rollback();
                 e.printStackTrace(); 
              }finally {
                 session.close(); 
              }
           }

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/licenta
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">root</property>
        <mapping resource="user.hbm.xml" />

    </session-factory>
</hibernate-configuration>

用户映射:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="models.User" table="user">
        <meta attribute="class-description">
            This class contains the user detail.
        </meta>
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>

        <property name="username" column="username" type="string" />
        <property name="password" column="password" type="string" />
        <property name="address" column="address" type="string" />
        <property name="email" column="email" type="string" />
        <property name="phone" column="phone" type="string" />
        <property name="year" column="year" type="int" />
        <property name="type" column="type" type="string" />
    </class>
</hibernate-mapping>

Pom文件:

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.paul.licenta</groupId>
    <artifactId>teachApp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>teachApp</name>

    <build>
        <finalName>teachApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.0.0.ga</version>
        </dependency>

        <dependency>
               <groupId>org.hibernate</groupId>
               <artifactId>hibernate-annotations</artifactId>
               <version>3.3.0.GA</version>
         </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
           <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>
        <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
         <groupId>commons-logging</groupId>
         <artifactId>commons-logging</artifactId>
         <version>1.1.1</version>
    </dependency>
        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.10.Final</version>
    </dependency>

    <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>2.26-b03</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

错误:

Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
    at dao.UserDAO.listUsers(UserDAO.java:58)
    at Main.main(Main.java:31)

另外,如果你看一下,在main方法中,我有2个工厂对象实例,其中一个是注释的。如果我取消注释并评论第二个,它会给我带来同样的错误。

有什么建议吗?

Here is everything the console shows at running

从UserDAO类导入:

import java.util.Iterator;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import models.User;
import net.sf.ehcache.hibernate.HibernateUtil;

从主要类别进口:

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import dao.UserDAO;
import models.User;

1 个答案:

答案 0 :(得分:1)

导入Session类错误,请使用 import org.hibernate.Session ,而不是 org.hibernate.classic.Session