SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2146)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2127)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2107)
at hibernate.TestUser.main(TestUser.java:14)
编译如下的test文件报上面的错误,
package hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class TestUser {
public static void main(String[] args) {
try {
SessionFactory sf=new Configuration().configure().buildSessionFactory();//错误提示的14行代码。
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
User user=new User();
user.setUsername("Hibernate");
user.setPassword("123");
session.save(user);
ts.commit();
session.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*************************************************************************
hibernate.cfg.xml是同一包hibernate下的配置文件。
------解决方案--------------------
hibernate.cfg.xml直接放在src下
------解决方案--------------------
方法1:
方法二: 将代码改为 ,其中path 是存放hibernate.cfg.xml 文件的路劲
String path="";
SessionFactory sf=new Configuration().configure(path).buildSessionFactory();/