hibernate-distribution-3.6.5.Final 包目录下
必须使用的包
*required文件夹下所有的包
*hibernate3.jar 是hibernate 核心包
*jdbc包 因为hibernate 是对jdbc包的封装,所以必须有jdbc包,这里可以是 mysql ,或者sqlserver或者是oracle 的jdbc驱动
*jpa目录下hibernate-jpa-2.0-api-1.0.0.Final.jar ,如果没有这个包就会报错NoClassDefFoundError: javax/persistence/EntityListeners
使用用户自定义library
window --preferences--java--buildpath--User Libraries 建立自定义的包 ,然后把上面的包导入进来
引用自定义的library
点击项目,右键properties---java build path--library---addlibrary---User library,选择刚才创建的包。
使用这种方法的好处是能看到hibernate 包里面的目录结构,以及dtd 文件。
dtd 错误
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
有2种dtd 写法 到底选哪个 ,这要看hibernate.jar 里面到底是怎么写的
点击org.hibernate包目录下 ,有hibernate-configuration-3.0.dtd文件和hibernate-mapping-3.0.dtd 文件 ,看看 hibernate.jar文件是怎么写的就可以了把他们复制过来绝对不会错。
如果dtd 使用了另外一个 ,当你有网络的时候,在本地没有找到dtd 会到网上下载一个dtd ,所有不会报错,但是在没有网络的情况下,找不错dtd 可能会报错,
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml。
Error on line 1 of document http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd : The markup declarations contained or pointed to by the document type declaration must be well-formed. Nested exception: The markup declarations contained or pointed to by the document type declaration must be well-formed.
不管是否会出现这样的错误,还是要按照包所提供的dtd 来写。
配置hibernate 还是使用myeclipse 好,因为写配置文件时有智能提示,eclipse 没有。
测试时
<property name="connection.url">jdbc:mysql://localhost:3306/first1</property>
这里我连接的是mysql 数据库 ,注意这里的first1 是数据库名,hibernate 不会帮你生成数据库名得,他只会帮你创建表,所有你要先手动创建数据库。
使用mysql 还是很方便的,因为比较小,下面介绍下使用mysql 常见命令
cmd
mysql -u root -p root //-u root root是用户名 -p root root是密码
show databases; 显示所有的数据库
use first1 使用first1 数据库
show tables 显示数据库下面所有的表
desc user 显示user 表结构
按照这样做问题不大了,下面就是按照帮助文档里面提供的来一步一步实现即可
hibernate-distribution-3.6.5.Final\documentation\manual\zh-CN\html
看中文的帮助文档还是很不错的,因为翻译的很好,而且写的很详细,但是当你打开的时候是乱码
解决办法是点击浏览器的查看 编码 选择unicode utf-8 就能显示中文了。