“hibernate.cfg.xml not found”却不是hibernate.cfg.xml的位置的原因,到底怎么回事啊(急啦。。。)

spree1010 2004-11-12 03:17:28
初学hibernate,写个简单例子,没用tomcat,是个单机的例子,总是出错,请大虾们指教,步骤如下

1,准备工作:将
mysql-connector-java-3.0.14-production-bin.jar
jta.jar
hibernate2.jar
cglib-full-2.0.2.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.4.jar
ehcache-0.9.jar
log4j-1.2.8.jar
odmg-3.0.jar
放在%JAVA_HOME%\jre\lib\ext\下面,并将每一个.jar在环境变量CLASSPATH中设置

2,写一个用来persist的类 Customer.java
public class Customer {
private String id;
private String name;

public String getId() {
return id;
}

public String getname() {
return name;
}

public void setId(String string) {
id = string;
}

public void setName(String string) {
name = string;
}
}

3,在Mysql中建一个表,表名为customer
CREATE TABLE CUSTOMER (
user_id CHAR(32) NOT NULL PRIMARY KEY,
name VARCHAR(16) NOT NULL, );

4,为Customer类和customer表写个映射文件,Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="Customer" table="CUSTOMER">
<id name="id" type="string" unsaved-value="null">
<column name="user_id" sql-type="char(32)" />
<generator class="uuid.hex"/>
</id>
<property name="name" type="string" not-null="true">
<column name="username" length="16" not-null="true"/>
</property>
</class>
</hibernate-mapping>

5,写一个简单的测试文件 HibernateTest.java,其中用jdbc负责数据库的连接部分
import java.sql.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateTestAgain
{
public static void main(String[] args) throws HibernateException
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/test";
String username="root";
String password="root";
conn=DriverManager.getConnection(url,username,password);
}
catch(Exception e){}

Configuration cfg = new Configuration();
cfg.addResource("Customer.hbm.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session= sessionFactory.openSession(conn);
Customer customer = new Customer();
customer.setName("caterpillar");
customer.setSex('M');
customer.setAge(28);


Transaction tx= session.beginTransaction();
session.save(customer);
tx.commit();
session.close();
sessionFactory.close();

System.out.println("新增資料OK!請先用MySQL觀看結果!");
}
}

到了这一步都是成功的。然后下一步问题就出现了

6,写一个hibernate.cfg.xml来负责数据库的连接
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<mapping resource="Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>


7,再次测试,将HibernateTest.java改为(即数据库连接的部分和添加的Customer的属性改变了)
import java.sql.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateTestAgain
{
public static void main(String[] args) throws HibernateException
{
SessionFactory sessionFactory = new Configuration().configure ().buildSessionFactory();

Session session= sessionFactory.openSession(conn);
Customer customer = new Customer();
customer.setName("kenshin");
customer.setSex('F');
customer.setAge(56);


Transaction tx= session.beginTransaction();
session.save(customer);
tx.commit();
session.close();
sessionFactory.close();

System.out.println("新增資料OK!請先用MySQL觀看結果!");
}
}


8,上面的所有文件都在一个目录下

9,执行后
/hibernate.cfg.xml not found 可恶的错误又出现了。。。。。。。。。。。。。
我看了文档,应该没问题啊,到底是怎么了????
郁闷我好几天了,希望大侠能亲手调试一下。



...全文
1812 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
kehua_gao 2004-11-22
  • 打赏
  • 举报
回复
我也遇到同样问题,我的IDE是JBuilder2005。hibernate.cfg.xml放在web-inf下,也试过放在web-inf/classes,还试过放在其它目录下都说找不到。
用new Configuration().configure("c:/xxx/xxx/xxx/....xml").buildSessionFactory();
还是找不到

SessionFactory sessionFactory = new Configuration()
.configure(new File(“hibernate.cfg.xml”)).buildSessionFactory();
又说找不到Cat.hbm.xml
真搞不懂到底这些文件应该放在哪里?
pbMaster 2004-11-13
  • 打赏
  • 举报
回复
如果没有问题的话,就证明你的位置的确放错了。
如果还是有问题的话,就把电脑砸了!
spree1010 2004-11-13
  • 打赏
  • 举报
回复
现在我改成
SessionFactory sessionFactory = new Configuration()
.configure(new File(hibernate.cfg.xml)).buildSessionFactory();
就ok了,这是什么原因呢?????
pbMaster 2004-11-13
  • 打赏
  • 举报
回复
你干脆用硬编码指定XML的位置!
就是在
new Configuration().configure("c:/xxx/xxx/xxx/....xml").buildSessionFactory();
pbMaster 2004-11-13
  • 打赏
  • 举报
回复
如果你确保你的目录是正确的,那你只好把HIBERNATE相关的原代码中加入调试代码看看它到底搞了什么鬼。
spree1010 2004-11-13
  • 打赏
  • 举报
回复
to pbMaster(编程无境界) :

用硬编码指定XML的位置确实可是

可为什么不能自动搜索到hibernate.cfg.xml文件呢???
jack9491 2004-11-12
  • 打赏
  • 举报
回复
把你的hibernate.cfg.xml放到你的classes目录下应该就ok啦
IceCraft 2004-11-12
  • 打赏
  • 举报
回复
兄弟既然学习Hibernate,那必定有一定的Java基础了,怎么不照这hibernate中文文档的与猫同乐作例子学习呢,顺便学习这个文档,更好的认识hibernate阿!不要困在这个问题上浪费了时间,耽误了学习。每个开发者的开发环境都有可能不同,即使在一个很微小的地方配置错误也可能产生各种问题,所以大家光凭你给出的这些信息还是很难断定你的问题所在。

来吧,走近Hibernate:
hibernate中文主站:http://www.hibernate.org.cn
主站入门:http://www.hibernate.org.cn/48.html
hibernate中文论坛:http://forum.hibernate.org.cn/
spree1010 2004-11-12
  • 打赏
  • 举报
回复
自己顶一下,高手来帮忙啊
mudsong 2004-11-12
  • 打赏
  • 举报
回复
哈哈,到www.javaeye.com上查查,祝你好运
spree1010 2004-11-12
  • 打赏
  • 举报
回复
to:mudsong(寻找可能)(江西上饶)
包应该都导入了,要不然第5步不能成功阿,是么??

我没用IDE工具
mudsong 2004-11-12
  • 打赏
  • 举报
回复
你的IDE工具是什么?
mudsong 2004-11-12
  • 打赏
  • 举报
回复
jta.jar
hibernate2.jar
cglib-full-2.0.2.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.4.jar
ehcache-0.9.jar
log4j-1.2.8.jar
odmg-3.0.jar

你有没有确认这么几个包没有正确导入?
spree1010 2004-11-12
  • 打赏
  • 举报
回复
第三步,纠错 应为
CREATE TABLE CUSTOMER (
user_id CHAR(32) NOT NULL PRIMARY KEY,
username VARCHAR(16) NOT NULL, );

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧