---
title: "hibernate的尝试"
author: "voff12"
date: "2007-01-13T18:59:51Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2007-01-13T18:59:51Z"
id: 46855
url: "https://voff12.iteye.com/blog/46855"
---

本人是从php进行web编程，转换为java编程。想学习一下hibernate的使用。大多数应用hibernate，都是用IDE工具来开发。我想就用简单的cmd来完成，但发现了用并不顺畅。做了一个简单的测试来，在编译Test类。
```
package chen.hibernate;

import chen.hibernate.User;
//import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;

public class   Test {
	public static void main(String[] args) {
		try
		{
			SessionFactory sf = new Configuration().configure().bulidSessionFactory();
			
			Session session  = sf.openSession();

			Transaction tx = session.beginTransaction();

			
            for (int i=0;i<3000;i++)
            {
		    User user = new User();
			user.setUsername("hibernate");
			user.setPassword("123");
			session.save(user);
                        }
			tx.commit();
			session.close();
			
			System.out.println("ok");
			
		}
		catch (HibernateException e)
		{
			e.printStackTrace();
		}

 }
}
```


的时候，总是出错。老是提示找不到符号。

```
 方法 bulidSessionFactory()
位置： 类 org.hibernate.cfg.Configuration
                        SessionFactory sf =
dSessionFactory();

Test.java:19: 找不到符号
符号： 类 Transation
位置： 类 chen.hibernate.Test
                        Transation tx = sess
                        ^
Test.java:34: 找不到符号
符号： 类 HibernateException
位置： 类 chen.hibernate.Test
                catch (HibernateException e)

```

buildSessionFactory()找不到类。初步估计是因为hibernate的包没有引用进来。
决定用google来查一下，看有没有有用的帮助。
关键词为hibernate+找不到符号符号：
花了整整一个下午，没找到原因。头好痛。
半小时后，发现一个字符错误。应Transaction。还剩两个错误

引入一个import org.hibernate.HibernateException后，错误减少为一个。
用javac -verbose chen/hibernate/Test.java后发现这些包都已经导入。


```
Test.java:16: 找不到符号
符号： 方法bulidSessionFactory()
位置：  类 org.hibernate.cfg.Configuration
                        SessionFactory sf = new Configuration().confi
                                            ^
gure().bulidSessionFactory();
```

初步原因分析：
 SessionFactory sf = new Configuration().configure().bulidSessionFactory()找不到符号，可能因为configuration没引入，没有配置对？
Configuration是hibernate的入口.

三个小时后：

```
  Configuration config = new Configuration().configure();      
		  SessionFactory sessionFactory = config.buildSessionFactory();  
		  Session session = sessionFactory.openSession(); 
			Transaction tx = session.beginTransaction();
```

终于编译能过。
但新问题又出现了：
Error parsing XML: XML InputStream
加上
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
解决了这一问题。但
新的问题又出现了

```
log4j:WARN No appenders could be find for logger
log4j:WARN please initialize the log4j system properly
```


解决此问题,在hibernate.cfg.xml旁加log4j.properties配置文件。
运行时会出现：很多详细信息。
又出一问题：

```

Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
  at java.lang.Class.getDeclaredConstructors0(Native Method)

```


第二天中午查原因：
到这一步原因已经明显，hibernate2.0与3.0的包不同
在google的途中，看到了这样一句话：

```
多看文档，多思考，自己解决问题。
多写代码，多锻炼，程序不费力气。
```

还有点道理，有问题，一定要自己解决。

```

NoClassDefFoundError: org/objectweb/asm/Type 

```

下一个问题：

```

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/Synchronization
```

导入asm.jar,asm-attrs.jar,jta.jar,ehcache-1.2.3.jar,log4j-1.2.11.jar
就解决了以上问题。
终于成功了。
总结;
1)将hibernate必要的包放到classpath中。
2)将hibernate.cfg.xml放至classes下。
3)User.hbm.xml与User.class放到同一包中。
4)log4j.properties一定要添加，可以看到很多详细信息
5)解决中文乱码，采用?useUnicode=true&amp;characterEncoding=UTF-8