Java NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
从一段时间以来,我一直在与Hibernate打交道,试图从我的数据库中访问数据。我搜索了很多错误,尝试了很多事情,只是不知道还有什么我做不到,我尝试了一些关于pom依赖性的事情,但是没有任何效果。这是: 主要类别:Java NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;,java,hibernate,maven,Java,Hibernate,Maven,从一段时间以来,我一直在与Hibernate打交道,试图从我的数据库中访问数据。我搜索了很多错误,尝试了很多事情,只是不知道还有什么我做不到,我尝试了一些关于pom依赖性的事情,但是没有任何效果。这是: 主要类别: public class Main { private static SessionFactory factory; public static void main(String[] args) { // User u = new User(
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>
另外,如果你看一下,在main方法中,我有两个factory对象的实例,一个是注释的。如果我取消那个注释并注释第二个,它会给我同样的错误
有什么建议吗
从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;
会话类的导入错误,请使用import org.hibernate.Session,而不是org.hibernate.classic.Session,检查会话工厂n其他相关导入…不应使用经典导入可以查看导入吗?我可能没有支付足够的费用,但我真的没有看到任何与经典相关的导入。一些错误的依赖关系也可能导致此问题……请在项目资源中搜索org.hibernate.classic.Session类。检查其来源,然后尝试升级,删除依赖项我现在正在搜索。同时,您是否认为此错误可能是由于错误使用hibernate或类似版本造成的?请在配置依赖项时查看此链接:-
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)
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;