“hibernate.cfg.xml not found”却不是hibernate.cfg.xml的位置的原因,到底怎么回事啊(急啦。。。)
初学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 可恶的错误又出现了。。。。。。。。。。。。。
我看了文档,应该没问题啊,到底是怎么了????
郁闷我好几天了,希望大侠能亲手调试一下。