gst*_*low 21 java orm hibernate
我创建了一个具有以下结构的项目:
HibernateUtil中:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\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);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
Run Code Online (Sandbox Code Playgroud)
在线
Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
Run Code Online (Sandbox Code Playgroud)
我有错误
初始SessionFactory创建failed.org.hibernate.HibernateException:C:\ Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml未找到线程"main"中的异常java.lang.ExceptionInInitializerError at logic.HibernateUtil.buildSessionFactory (HibernateUtil.java:19)在logic.HibernateUtil.(HibernateUtil.java:9)处于logic.Main.main(Main.java:12)引起:org.hibernate.HibernateException:C:\ Users\Nikolay_Tkachev\workspace \在org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1947)的org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)中找不到hiberTest\src\logic\hibernate.cfg.xml org.hibernate.cfg.Configuration.configure(Configuration.java:1928)在logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)......还有2个
错误的原因是什么,我该如何解决?
Sur*_*tta 31
提供相对于项目的路径.
创建一个resources
在您的名字中 调用的文件夹src
并将配置文件放在那里
configuration.configure("/resources/hibernate.cfg.xml");
Run Code Online (Sandbox Code Playgroud)
如果你检查你的代码
Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
return new Configuration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
在两行中,您将创建两个配置对象.
如果你写,这应该工作(没有测试),
Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
return configuration.buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
但是在服务器上部署后它会失败,因为您使用的是系统路径而不是项目相对路径.
Kis*_*ram 12
不知何故放在"src"文件夹下对我来说不起作用.
而是将cfg.xml放置如下:
[Project Folder]\src\main\resources\hibernate.cfg.xml
Run Code Online (Sandbox Code Playgroud)
工作.使用此代码
new Configuration().configure().buildSessionFactory().openSession();
Run Code Online (Sandbox Code Playgroud)
在一个文件下
[Project Folder]/src/main/java/com/abc/xyz/filename.java
Run Code Online (Sandbox Code Playgroud)
另外在hibernate.cfg.xml中有这段代码
<mapping resource="hibernate/Address.hbm.xml" />
<mapping resource="hibernate/Person.hbm.xml" />
Run Code Online (Sandbox Code Playgroud)
将上面的hbm.xml文件放在:
编辑:
[Project Folder]/src/main/resources/hibernate/Address.hbm.xml
[Project Folder]/src/main/resources/hibernate/Person.hbm.xml
Run Code Online (Sandbox Code Playgroud)
以上结构有效.
归档时间: |
|
查看次数: |
104207 次 |
最近记录: |