![](/img/trans.png)
[英]Exception in thread “main” org.hibernate.HibernateException: hibernate.cfg.xml not found
[英]org.hibernate.HibernateException: /hibernate.cfg.xml not found
我正在嘗試在Spring 3 MVC中使用休眠模式,但此刻我拋出了此異常。 我想我需要在某個地方定義hibernate.cfg.xml
,但不確定在哪里?
我基本上在這里遵循了這個示例, 網址為http://www.nabeelalimemon.com/blog/2010/05/spring-3-integrated-with-hibernate-part-a/特別是在那里看到的這行代碼應該“神奇地”使用以下命令找到我的hibernate.cfg文件:
return new Configuration().configure().buildSessionFactory();
我猜這是不正確的? 我目前在src/com/jr/hibernate/
有我的hibernate.cfg文件
以下是我的cfg文件:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/racingleague</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="hibernate.format_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--property name="hbm2ddl.auto">update</property-->
<mapping resource="com/jr/model/hibernateMappings/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的休眠實用程序類:
package com.jr.utils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
public static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
這個抽象類被稱為bu:
package com.jr.db;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import com.jr.utils.HibernateUtils;
public abstract class DbWrapper<T> {
private static SessionFactory sessionFactory = null;
private static Session session;
public DbWrapper() {
setSessionFactory();
}
private void setSessionFactory() {
sessionFactory = HibernateUtils.buildSessionFactory();
session = sessionFactory.getCurrentSession();
}
public boolean addNewItem(T dbItem) {
try {
session.getTransaction().begin();
session.save(dbItem);
session.getTransaction().commit();
} catch (Exception e) {
System.err.println("error exception when adding new item to table"
+ e);
} finally {
session.close();
sessionFactory.close();
}
return false;
}
public abstract boolean removeItem(String uid);
public abstract boolean modifyItem(String uid, T item);
}
這是最初執行一些休眠操作的控制器:
private Logger logger = Logger.getLogger(UserController.class);
private UserDb userDb;
@RequestMapping(value = "/user/registerSuccess", method = RequestMethod.POST)
public String submitRegisterForm(@Valid User user, BindingResult result) {
// validate the data recieved from user
logger.info("validate the data recieved from user");
if (result.hasErrors()) {
logger.info("form has "+result.getErrorCount()+" errors");
return "account/createForm";
} else{
// if everthings ok, add user details to database
logger.info("if everthings ok, add user details to database");
userDb = new UserDb();
userDb.addNewItem(user);
// display success and auto log the user to the system.
return "account/main";
}
}
提前加油。 我的所有表hibvernate xml映射都與hibernate.cfg.xml文件位於同一位置
不要將hibernate.cfg.xml
文件放在src/com/jr/hibernate/
目錄下,而是將其放在src
目錄下。 然后,它將自動出現在WEB-INF/classes
目錄中,如此處的人員所提到的。
啟動webapp時,必須在類路徑的根目錄中找到hibernate.cfg.xml
。
如果您正在使用maven構建項目,請將hibernate.cfg.xml放在src/main/resources
目錄中,以便在構建war軟件包時將其自動放置在/WEB-INF/classes
。
如果不使用maven,則將文件直接放在WEB-INF/classes
目錄中。
hibernate.cfg.xml
應該在WEB-INF/classes
。 另外,您可以通過將相應的參數傳遞給configure(..)
方法來從自定義位置加載它。
如果使用Maven,則應將文件hibernate.cfg.xml放在Intellij IDEA中的以下路徑/src/main/java/resources/hibernate.cfg.xml中。 然后,在您的運行應用程序類中插入以下行:
SessionFactory factory = new Configuration()。configure(“ hibernate.cfg.xml”)。addAnnotatedClass()。buildSessionFactory();
在IntelliJ中,轉到“打開項目設置” >>“模塊” >>“ Hibernate”,然后將您的項目中使用的hibernate.cfg.xml文件定位為目標。
我有同樣的問題,並將hibernate.cfg.xml移到src / main / resources目錄中已解決,它將被自動放置在/ WEB-INF / classes中。
即使我在src
文件夾下有hibernate.cfg.xml
,我也得到了
org.hibernate.HibernateException: /hibernate.cfg.xml not found
運行mvn clean install
。 通過嘗試和錯誤,我能夠通過從src
折疊中刪除hibernate.cfg.xml
並將其添加到其他地方來解決該問題。 運行應用程序(在我的情況下這是一個主類)。 在此期間,我仍然會收到錯誤消息。 並將其添加回src
文件夾並朗讀主類。 It worked!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.