即使添加了Hibernate,它也没有被导入

时间:2017-05-24 12:05:39

标签: java mysql eclipse hibernate maven

这是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行代码中评论过,向您展示我在哪里得到错误。实际上我几乎所有与Hibernate相关的命令都会出错,比如SessionFactory,Transaction,Commit等。

但是我添加了Hibernate和mysql Connector的依赖。

我在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>

有人可以在我错的地方帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

我无法从您的pom.xml片段中判断出来,但是您可能将这些依赖项放在<dependencyManagement>部分而不是<dependencies>部分中?

相关问题