posts - 72, comments - 19, trackbacks - 0, articles - 0

hibernate学习备忘

Posted on 2006-04-24 14:57 阅读(110) 评论(0)  编辑 收藏 引用 所属分类: java学习日记
1、在hibernate.cfg.xml 中mapping映射实体类时,如果不在同一目录下,例如:实体类的映射文件Userlist.bhm.xml在com\sonic\目录下,而hibernate.cfg.xml 在com同一级目录下,应写成
<mapping  resource="com/sonic/Userlist.hbm.xml"/>
不能用包名或\分隔逐级目录
2、 Configuration().configure()
Configuration是hibernate的入口,在新建一个Configuration的实例的时候,hibernate会在classpath里面查找hibernate.properties文件,如果该文件存在,则将该文件的内容加载到一个Properties的实例GLOBAL_PROPERTIES里面,如果不存在,将打印信息hibernate.properties not found
  然后是将所有系统环境变量(System.getProperties())也添加到GLOBAL_PROPERTIES里面( 注1)。如果hibernate.properties文件存在,系统还会验证一下这个文件配置的有效性,对于一些已经不支持的配置参数,系统将打印警告信息。

  3、configure()在做什么?
    configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印如下信息并抛出HibernateException异常。  hibernate.cfg.xml not found

  如果找到该文件,configure()方法会首先访问< session-factory >,并获取该元素的name属性,如果非空,将用这个配置的值来覆盖hibernate.properties的hibernate.session_factory_name的配置的值,从这里我们可以看出,hibernate.cfg.xml里面的配置信息可以覆盖hibernate.properties的配置信息。

  接着configure()方法访问<session-factory>的子元素,首先将使用所有的<property>元素配置的信息( 注2),如前面我们使用的配置文件 <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>  会覆盖hibernate.properties里面对应的配置,hibernate2.1发布包里面自带的hibernate.properties文件(位于%HIBERNATE_HOME%/etc下面)里面的值,如下:hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:hsql://localhost

然后configure()会顺序访问以下几个元素的内容

<mapping>
<jcs-class-cache>
<jcs-collection-cache>
<collection-cache>
只有注册用户登录后才能发表评论。