【问题标题】:Even though Hibernate is added it is not being imported即使添加了 Hibernate,它也没有被导入
【发布时间】:2017-10-24 18:33:39
【问题描述】:

这是一个 Hibernate 的 SessionUtil 我得到错误:

import org.hibernate.Session;         //Error in here
import org.hibernate.SessionFactory;  //Error in here
import org.hibernate.cfg.Configuration;//Error in here


public class SessionUtil {

    private static SessionUtil instance=new SessionUtil();
    private SessionFactory sessionFactory;

    public static SessionUtil getInstance(){
            return instance;
    }

    private SessionUtil(){
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");

        sessionFactory = configuration.buildSessionFactory();
    }

    public static Session getSession(){
        Session session =  getInstance().sessionFactory.openSession();

        return session;
    }
}

我已在第 3 行代码中进行了注释,以向您展示我在哪里出错。实际上,我几乎在任何与休眠相关的命令(如 SessionFactory、Transaction、Commit 等)上都会出错。

但是我添加了 Hibernate 和 mysql 连接器的依赖。

我在 pom.xml 中添加的依赖项在这里::

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.34</version>
</dependency>

在资源中我还添加了 hibernate.cfg.xml::

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="hibernateSessionFactory">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb</property>
  <property name="hibernate.connection.username">user</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
<!--   <property name="hibernate.hbm2ddl.auto">create</property> -->
  <mapping class="com.example.model.User"/>
 </session-factory>
</hibernate-configuration>

谁能帮我解决我的错误。谢谢。

【问题讨论】:

  • 清理项目。可能是依赖项没有包含在类路径中
  • 你能在你的类路径中看到 jars 吗?
  • 请粘贴您的堆栈跟踪或日志文件,以便我们与您一起调查错误。
  • 是的,所有的 jars 都是在 maven Dependencies 中导入的,我可以看到所有的 jars,甚至是 hibernate-core。
  • 执行mvn eclipse:clean 并检查

标签: java mysql eclipse hibernate maven


【解决方案1】:

我无法从您的 pom.xml 片段中看出,但也许您将这些依赖项放在 &lt;dependencyManagement&gt; 部分而不是 &lt;dependencies&gt; 部分?

【讨论】:

  • 所有依赖项都在 部分内。
猜你喜欢
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多