Hibernate可以说伴随着我的整个Java历程。从我一开始工作,我就在一次意外的机会认识了它。以前,在使用语言上我是非常激进的。因为公司不采用Hibernate,我辞去了两份工作。可今天我们搞门户网站的时候,我却第一个反对使用Hibernate。总之,Hibernate让我彻夜难眠。
刚开始接触Hibernate时,自然就要接触Hibernate配置文件,众所周知,Hibernate有两种配置模式:一个采用属性文件,一个采用XML配置文件。一般在早期Hibernate2时,大家都喜欢采用属性文件,现在大家都喜欢用XML配置文件。现在不妨贴一段基本XML配置给大家看看:
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="connection.url">
- jdbc:mysql://localhost:3306/hibtest
- </property>
- <property name="connection.username">root</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="hibernate.fetch">true</property>
- <property name="hibernate.show_sql">true</property>
- <property name="hibernate.cache.use_query_cache">true</property>
- <property name="hibernate.cache.provider_class">
- org.hibernate.cache.EhCacheProvider
- </property>
- <mapping resource="hib/pojo/Canguan.hbm.xml" />
- <mapping resource="hib/pojo/Kecheng.hbm.xml" />
- <mapping resource="hib/pojo/Reply.hbm.xml" />
- <mapping resource="com/edong/pojo/SongGame.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
上面这段代码,在一般的Hibernate使用者来说再也普通不过了。但如果从本质上去看待这些文件,能够看到很多内容。
(1)作为一个XML首先要注意的就是它的结构。 Hibernate 结构是由hibernate-configuration-3.0.dtd决定的。打开dtd文件自然就可以看到dtd结构。由于篇幅限制,读者可以在源码的src\org\hibernate目录下找到该文件。如果用户对这些dtd不熟悉,可以去看源码\etc目录下的配置demo。如果你还觉得不够, 可以采用Eclipse的提示去达到自己的目的。
(2)大家要注意的是,作为一个配置文件开始的第一个标签是不动的。同样对于以上的配置文件来说,<hibernate-configuration>元素是全局元素。
先吃饭去了,过会儿再继续