我的测试类
package test;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.computer.cs.entity.Users;
public class Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
public void init(){
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory=config.buildSessionFactory(serviceRegistry);
session=sessionFactory.openSession();
transaction=session.beginTransaction();
}
public void destory(){
transaction.commit();
session.close();
sessionFactory.close();
}
public void test(){
Users u = new Users("ee","1234567",(short)1,322);
session.save(u);
}
public static void main(String[] args) {
Test t1 = new Test();
t1.init();
t1.test();
t1.destory();
}
}
下面是报错
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1947)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1928)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at test.Test.init(Test.java:18)
at test.Test.main(Test.java:40)
hibernate配置文档
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/csdatabase
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
CSdatabase
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/computer/cs/entity/Users.hbm.xml" />
<mapping resource="com/computer/cs/entity/Inexp.hbm.xml" />
<mapping resource="com/computer/cs/entity/Project.hbm.xml" />
</session-factory>
初学框架,现在真的是一脸懵逼