在Hibernate 4中构建Hibernate sessionFactory时找不到hibernate配置文件

时间:2015-10-10 21:46:27

标签: java hibernate hibernate-4.x

我有一个简单的项目结构 [src] [main] [resouces] 也就是说,src文件夹有主文件夹,它有资源文件夹,在resources文件夹里面,我有我的hibernate.cfg.xml。以下是文件:

    if (false === strtotime($dateInput)) {
        echo 'Sorry, what?';
    }

HibernateManger.java类

 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>
        <!-- connection settings-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/HibTut</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="show_sql">true</property>
        <!--connection pool size-->
        <property name = "connection.pool_size">1</property>
        <!-- mapping resource -->
        <mapping resource= "user.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

不幸的是,编译器抱怨&#34; resources / hibernate.cfg.xml&#34;找不到。

public class HibernateSessionManager {

    private static SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            if (sessionFactory == null)
            {
                Configuration configuration = new Configuration();
                configuration.configure("/resources/hibernate.cfg.xml");
                StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(ssrb.build());
            }
            return sessionFactory;
        } catch (Throwable ex)
        {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }

    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}

1 个答案:

答案 0 :(得分:0)

我现在解决了!我刚刚更改了这行 configuration.configure(&#34; /resources/hibernate.cfg.xml"); 对此: 的 configuration.configure(&#34;的hibernate.cfg.xml&#34);

现在可行。