Hibernate 配置Hibernate给人的感受是灵活的,要达到同一个目的,我们可以使用几种不同的办法。就拿Hibernate配置来说,常用的有如下三种方式,任选其一。
1.
2.
hibernate.properties可以非常的简洁明了,并且有一种linux配置文件的风格。以#开始一行的注释,用键值对的方式存储配置参数。
Configuration类
org.hibernate.cfg.Configuration实例的作用是对Hibernate进行配置,以及对它进行启动。在Hibernate的启动过程中,Configuration类的实例首先读取Hibernate配置文件,加载配置信息,然后加载映射文件,创建一个SessionFactory对象。
·
· hbm.xml或pojo类
Configuration常用操作函数
1.加载Hibernate配置文件
Configuration cfg=new Configuration().configure("/etc/hibernate.cfg.xml");
Configuration cfg=new Configuration().configure("/etc/hibernate.properties");
2.为Configuration指定映射文件
cfg.addResource("test/User.hbm.xml");
3.为Configuration指定POJO类,Order.hbm.xml根Order.java一个目录
cfg.addClass(test.Order.class);
4.为Configuration指定Hibernate配置属性,
1 2 3 4 5 | Configuration cfg = new Configuration() .addClass(test.User.class) .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect") .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test") .setProperty("hibernate.order_updates", "true"); |
5.获得SessionFactory
SessionFactory sessions = cfg.buildSessionFactory();
Hibernate 允许你的应用程序创建多个SessionFactory 实例。这对 使用多个数据库的应用来说很有用。
hibernate.cfg.xml
hibernate.cfg.xml在文档开头的DTD(文档类型定义)是很复杂的。我们并不需要去理会和记忆他。你可以直接copy它。
hibernate.cfg.xml文档以<hibernate-configuration>为根元素,你可以在其子元素<session-factory>中
·
·
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "www.hibernate.org/dtd/hiberna…"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">klguang@mysql</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">10</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <property name="javax.persistence.validation.mode">none</property> <mapping resource="hbm/User.hbm.xml" /> <mapping resource="hbm/Event.hbm.xml" /> <mapping resource="hbm/Person.hbm.xml" /> </session-factory> </hibernate-configuration> <br> |
hibernate.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 | #数据库使用的驱动类 hibernate.connection.driver_class=com.mysql.jdbc.Driver #数据库连接串 hibernate.connection.url=jdbc:mysql://localhost:3306/db #数据库连接的用户名 hibernate.connection.username=user #数据库连接的密码 hibernate.connection.password=password #数据库使用的方言 hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect #是否打印SQL语句 hibernate.show_sql=true javax.persistence.validation.mode=none |
hibernate.properties没有提供加载映射文件的方式。因此需要通过Configuration的.addResource()方法来加载映射文件或POJO类,Hibernate会自动找到另一方,前提映射文件和POJO类在同一包(目录)中。
1 2 3 4 | Configuration cfg = new Configuration(); cfg.configure("/etc/hibernate.properties"); cfg.addResource("test/User.hbm.xml"); cfg.addClass(test.Order.class); |
Hibernate配置参数详解
Hibernate JDBC 属性
属性名 | 用途 |
hibernate.connection.driver_class | JDBC driver class |
hibernate.connection.url | JDBC URL |
hibernate.connection.username | database user |
hibernate.connection.password | 数据库用户密码 |
hibernate.connection.pool_size | maximum number of pooled connections |
Hibernate 数据源属性
属性名 | 用途 |
hibernate.connection.datasource | 数据源
JNDI
名字
|
hibernate.jndi.url JNDI | 提供者的
URL
(可选)
|
hibernate.jndi.class JNDI | InitialContextFactory
类(可选)
|
hibernate.connection.username |
数据库用户(可选)
|
hibernate.connection.password | 数据库密码(可选) |
属性名 | 用途 | 可选值 ()内为默认 |
hibernate.dialect | 允许 Hibernate 针对特定的关系数据库生成优化的 SQL 的org.hibernate.dialect.Dialect 的类名。 例如:org.hibernate.dialect.MySQLDialect |
|
hibernate.show_sql | 输出所有 SQL 语句到控制台。 | true|false (false) |
hibernate.format_sql | 在 log 和 console 中打印出更漂亮的 SQL。 | true|false (false) |
hibernate.default_catalog | 在生成的 SQL 中,将给定的 catalog 附加于非全限定名的表名上 |
|
hibernate.session_factory_name | org.hibernate.SessionFactory 创建后,将自动使用这个名字绑定到 JNDI 中。 |
|
hibernate.max_fetch_depth | 为单向关联(一对一,多对一)的外连接抓取(outer join fetch)树设置最大深度。 | 0到3 |
hibernate.default_batch_fetch_size | 为 Hibernate 关联的批量抓取设置默认数量。 | 4、8、16 |
hibernate.default_entity_mode | 为由这个 SessionFactory 打开的所有 Session指定默认的实体表现模式。 | dynamic-map,dom4j,pojo |
hibernate.order_updates | 强制 Hibernate 按照被更新数据的主键,为SQL 更新排序。这么做将减少在高并发系统中事务的死锁。 | true|false
|
hibernate.generate_statistics | 如果开启,Hibernate 将收集有助于性能调节的统计数据。 | true|false
|
hibernate.use_identifier_rollback | 如果开启,在对象被删除时生成的标识属性将被重设为默认值。 | true|false
|
hibernate.use_sql_comments | 如果开启,Hibernate 将在 SQL 中生成有助于调试的注释信息,默认值为 false。 | true|false (false) |
Hibernate JDBC 和连接(connection)属性、Hibernate 缓存属性、Hibernate 事务属性等