Hibernate 5.3 (二)

本文详细介绍了Hibernate 5.3中的SessionFactory、Configuration、数据库连接属性、方言、常用配置属性及持久化类的相关概念。讨论了持久化类的状态转换、Hibernate映射文件的结构、集合映射的性能分析和映射数据库对象的方法。通过示例解释了持久化类的映射、主键和普通属性的映射,以及集合类型如List、Set和Map的映射策略。
摘要由CSDN通过智能技术生成

SeesionFactory

我们都知道Hibernate 进行持久化操作离不开SessionFactory对象,这个对象是整个数据库映射文件经过编译后的内存镜像,该对象的openSeesion方法可打开Session对象。该对象通常由Configuration对象的buildSessionFactory来产生。
方法

Configuration

每一个Hibernate 的配置都对应一个Configuration对象,在极端情况下,不使用任何配置文件,也可创建Configuration对象。

随着Hibernate 所使用的配置文件不同,创建Configuration的方式也不同。

  • 使用hibernate.properties文件作为配置文件。

这种配置方法可以去参考hibernate 文件包下的project/etc,给出每个常见数据库连接的属性。

 Configuration con1 = new Configuration();
	   con1.addResource("News.hbm.xml");

这种配置文件,可以看出没有添加映射文件的方式,所以必须手动通过addResource 方法去添加映射文件,这很费事。在实际开发的时候,不建议使用这种。

  • 使用Configure.cfg.xml 作为配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入hibernate配置文件的标签 -->
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 5.3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        <hibernate-configuration>
        <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="hibernate.c3p0.max_size">20</property>
		<!-- 指定连接池里最小连接数 -->
		<property name="hibernate.c3p0.min_size">1</property>
		<!-- 指定连接池里连接的超时时长 -->
		<property name="hibernate.c3p0.timeout">5000</property>
		<!-- 指定连接池里最大缓存多少个Statement对象 -->
		<property name="hibernate.c3p0.max_statements">100</property>
		<property name="hibernate.c3p0.idle_test_period">3000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<property name="hibernate.c3p0.validate">true</property>
		<!-- 指定数据库方言 -->
		<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		<!-- 根据需要自动根据映射文件创建数据表 -->
		<property name="hbm2ddl.auto">update</property><!--①-->
		<!-- 显示Hibernate持久化操作所生成的SQL -->
		<property name="show_sql">true</property>
		<!-- 将SQL脚本进行格式化后再输出 -->
		<property name="hibernate.format_sql">true</property>
		<!-- 罗列所有持久化映射文件-->
		<mapping resource="myhibernate.hbm.xml"/>//在这你就可以添加你映射的文件
        </session-factory>
        </hibernate-configuration>

Configuration con = new Configuration().configure();
  • 不用配置文件去创建Configuration 实例
// 实例化Configuration,不加载任何配置文件
		Configuration conf = new Configuration()
			con.addResource("News.hbm.xml");//添加映射类
			// 通过setProperty设置Hibernate的连接属性。
			.setProperty("hibernate.connection.driver_class"
				, "com.mysql.jdbc.Driver")
			.setProperty("hibernate.connection.url"
				, "jdbc:mysql://localhost/hibernate")
			.setProperty("hibernate.connection.username" , "root")
			.setProperty("hibernate.connection.password" , "32147")
			.setProperty("hibernate.c3p0.max_size" , "20")
			.setProperty("hibernate.c3p0.min_size" , "1")
			.setProperty("hibernate.c3p0.timeout" , "5000")
			.setProperty("hibernate.c3p0.max_statements" , "100")
			.setProperty("hibernate.c3p0.idle_test_period" , "3000")
			.setProperty("hibernate.c3p0.acquire_increment" , "2")
			.setProperty("hibernate.c3p0.validate" , "true")
			.setProperty("hibernate.dialect"
				, "org.hibernate.dialect.MySQL5InnoDBDialect")
			.setProperty("hibernate.hbm2ddl.auto" , "update");
jdbc 连接的常用属性
hibernate.dialect 	classname of org.hibernate.dialect.Dialect subclass
hibernate.connection.username	database username
hibernate.connection.password	database password
hibernate.connection.url 	JDBC URL (when using java.sql.DriverManager)
hibernate.connection.driver_class 	classname of JDBC driver
hibernate.connection.pool_size 	the maximum size of the connection pool (only when using java.sql.DriverManager)
hibernate.connection.datasource 	databasource JNDI name (when using javax.sql.Datasource)
hibernate.jndi.url	JNDI InitialContext URL
hibernate.jndi.class	JNDI InitialContext classname
hibernate.max_fetch_depth 	maximum depth of outer join fetching
hibernate.jdbc.batch_size 	enable use of JDBC2 batch API for drivers which support it
hibernate.jdbc.fetch_size 	set the JDBC fetch size
hibernate.hbm2ddl.auto 	enable auto DDL export
hibernate.default_schema 	use given schema name for unqualified tables (always optional)
hibernate.default_catalog 	use given catalog name for unqualified tables (always optional)
hibernate.session_factory_name 	If set, the factory attempts to bind this name to itself in the 

这里给出的是hiernate.properties里面配置属性,对于hibernate.cfg.xml和hibernate.properties两种设置属性不同,使用的自行百度查找,形式不同,本质一样。
我只是举出一些,具体的属性在Hibernate的api下environment.html。
Hibernate自带连接池仅有测试功能,在实际项目中,你还是需要使用,C3P0或者Proxool连接池,只需要做如下修改,代替hibernate.connection.pool_size即可:

		<property name="hibernate.c3p0.max_size">20</property>
		<!-- 指定连接池里最小连接数 -->
		<property name="hibernate.c3p0.min_size">1</property>
		<!-- 指定连接池里连接的超时时长 -->
		<property name="hibernate.c3p0.timeout">5000</property>
		<!-- 指定连接池里最大缓存多少个Statement对象 -->
		<property name="hibernate.c3p0.max_statements">100</property>
数据库方言

Hibernate 底层依然是使用标准的数据库进行操作的,所有的数据库都是支持SQL的标准的,但是在它上面进行了封装,所有语法上有了差异,因此Hibernate需要根据数据库来识别这些差异。我们在把项目进行迁移不同数据库的时候,访问数据库会有差异,我们必须告诉Hibernate我们使用的数据库,就是这样借助于数据库方言去实现的。

至于,每一种数据库对应的方言,在这我就不叙说,有问题,自己百度去查找。

常用的配置属性
  • hibernate.show_ sql: 是否在控制台输出Hibernate生成的SQL语句。只能为true和false。
  • hibernate. format sql: 是否将SQL语句转成格式良好的SQL。只接受true和false两个值。hibernate.use_ sql_ comments: 是否在Hibernate生成的SQL语句中添加有助于调试的注释。只接受true和false两个值。
  • hibernate.jdbc.fetch_ size: 指定JDBC抓取数量的大小,它可接受一个整数值,其实质是调用Statement.setFetchSize()方法。
  • hibernate.jdbc.batch_ size: 指定Hibernate使用JDBC2的批量更新的大小,它可接受一个整数值,建议取5到30之间的值。
  • hibernate.connection.autocommit: 设置是否自动提交。通常不建议打开自动提交。
  • hibernate.hbm2ddl.auto: 设置当创建SessionFactory时,是否根据映射文件自动建立数据库表。 如果使用create-drop值, 显示关闭SessionFactory时,将Drop刚建的数据表。该属性可以为update、create 和create-drop三个值。

这里我们需要主要说明一下:

create࿱

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值