姜子鱼钓牙 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

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)