姜子鱼钓牙 2018-11-27 19:44 采纳率: 0%
浏览 410

hibernate4.1 创建session 求助!

我的测试类
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>

图片说明

初学框架,现在真的是一脸懵逼

  • 写回答

1条回答 默认 最新

  • 爱码少年 00fly.online 2018-11-28 02:24
    关注

    去classes文件目录下面看看,有没有 hibernate.cfg.xml

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料