Hibernate配置说明
hibernate.cfg.xml格式如下: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--SessionFactory的配置信息-->
<!--mapping的配置信息-->
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml是hibernate用于连接数据库的文件,默认放在class目录下,Configuration需要根据此文件创造SessionFactory
Sessionfactory需要配置的元素属性有:
dialect:连接数据库的方言
connection.url:连接的url connection.
driver_class:连接的数据库驱动
connection.username:数据库登录名
connection.password:数据库登录密码
或者使用JNDI
connection.datasource:datasource名称
Sessionfactory其它的配置元素属性:
transaction.factory_class:指定一个Transaction实例工厂类
show_sql:是否在控制台上显示
SQL format_sql:是否按表准格式输出
SQL default_schema:在生成的SQL中,将给定的
schema/atblespace附加于非全限定名的表上
default_catalog:在生成的SQL中,将给定的
catalog附加于非全限定名的表上
session_factory_name:绑定JNDI的名称
max_fetch_depth:外联结抓取的最大深度
default_entity_mode:批量抓取的最大数量
order_updates:已更新数据主键为基准为SQL排序
generate_statistics:是否收集性能数据
user_identifer_rollback:对象被删除后表示是否被设置为默认值
_sql_comments:在SQL中输出注释信息
连接SQLserver2000的配置: <?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">
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.password">123</property>
<property name="connection.url"> jdbc:microsoft:sqlserver://localhost:1433 </property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class"> com.microsoft.jdbc.sqlserver.SQLServerDriver </property>
<!--mapping的配置信息-->
</session-factory>
</hibernate-configuration>
连接MySQL的配置: <?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">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url"> jdbc:mysql://localhost:3306/db_database </property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class"> com.mysql.jdbc.Drive </property>
<!--mapping的配置信息-->
</session-factory>
</hibernate-configuration>
连接Oracle的配置: <?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">
<hibernate-configuration>
<session-factory>
<property name="connection.username">SYSTEM</property>
<property name="connection.password">123</property>
<property name="connection.url"> jdbc:oracle:thin:@localhost:1521:db_database </property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.driver_class"> oracle.jdbc.driver.OracleDrive </property>
<!--mapping的配置信息--> </session-factory>
</hibernate-configuration>
连接使用JNDI的配置: <?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="connection.datasource"> java:comp/env/jdbc/TestDB </property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="myeclipse.connection.profile">JDBC</property>
<!--mapping的配置信息-->
</session-factory>
</hibernate-configuration>
XXX.hbm.xml格式如下: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools -->
<hibernate-mapping>
<class name="classname" table="tablename" schema="dbo" catalog="hibernatExercise">
<!--对象和数据影射的属性-->
</class>
</hibernate-mapping>
映射文件将被配置在hibernate.cfg.xml下mapping中,它将一张表对应一个持久化类
hibernatemapping配置元素属性: class元素的name属性:久化类名称 class元素的table属性:对应表的名称 id子元素:表主键和类属性的映射 property子元素:表字段和类属性的影射 generator子元素:对象表示的生成方式
Java Hibernate SQL数据类型对应关系:
String String varchar()
String text text
int int int
char character char()
boolean boolean bit
byte[] binary blob
Date date date
Timestamp timestamp timestamp
外键关联:
one-to-one 一对一
many-to-one 多对一
one-to-many 一对多
many-to-many 多对多
name 映射属性的名称
cascade 对关联对象的级联操作
lazy 是否进行延迟检索
column 映射为对应表的外键
class 映射的类型
<one-to-one name="right" class="com.bo.Student" cascade="all" lazy="false">
使用Spring配置管理sessionFactory的写法: <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/TestDB</value>
</property>
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>
<!--mapping的配置信息-->
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
如上所示由spring来管理sessionFactory.