hibernate 学习小结

             hibernate对jdbc做了轻量级的封装,使得我们程序猿直接以面向对象的思维操作数据库。hibernate单纯的使用起来很简单,但是如果什么都不了解在更改的时候会很麻烦。这里记录下一些基本概念方便以后使用

    hibernate 3的持久化状态有三种 transient(瞬时状态) 这个状态可以设置在一对多关系中一的一方,persistent(持久化状态)  和 detached(脱管状态)

    hibernate 3的框架核心为configuration,它负责启动hibernate 3 (这是单独把hibernate拿出来的用法,如果与spring集成则由spring负责启动),hibernate使用主要是通过sessionFactory获得session然后进行数据库操作,如果用config则 new Configuration().configure().buildSessionFactory()的方式获得该对象,如果spring集成的话只需要dao层实现类继承HibernateDaoSupport类直接获得即可。

    hibernate 3的主键生成机制

    Assigned方式表示人工手工生成主键

    Hilo 表示使用高低位算法生成

    Increment 方式表示对主键值采取自动增长方式生成新的主键值,但要求底层数据库支持Sequence

    Identity 方式表示根据底层数据库来支持自动增长,不同的数据库用不同的主键增长方式

    Sequence方式需要底层数据库支持Sequence方式,例如oracle数据库

    Native.方式表示根据不同的底层数据库自动选择Identity,Sequence,Hilo 主键的生成方式。

    UUID 方式表示使用128位UUID算法生成主键,能够保证网络环境下的主键唯一性,也能够保证在不同数据库及不同服务器下主键唯一性。

   hibernate 两种配置,一种是注解(Annotation) 一种是 xml ,大的工程最好用注解,注解的优先级高于xml(注解算是Hibernate对jpi的支持?),使用注解需要两个对注解提供支持的jar文件,分别是hibernate-annotations.jar 和 ejb3-persistence.jar,注解的方法可以避免那么多的xml文件所以建议大家用注解的方法。

  hibernate 数据库连接池 三种 DBCP,C390以及Proxool。

  hibernate 缓存机制:提供了2级cache 其中一级缓存是Session级别的缓存,二级缓存是SessionFactory 级别的缓存,他书句进程范围或群集范围的缓存,二级缓存可以配置和更改(笔者工程中有设置但是从来没用过- -)二级缓存是一次性获得所有数据对象,并根据ID放入到第二级缓存中,当Hibernate根据ID访问数据对象时首先从一级缓存中查询,如果查询不到,再从二级缓存中查询,如果还是查不到再从数据库查询,把结果按照Id放入到缓存中,删除更新,增加数据时同步更新缓存(CRUD操作是指create,retrieve,update,delete,这个概念小弟今天才知道,太专业了)。

    hibernate 和spring集成的时候设置比较简单,在这里说下直接上代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/jee
  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
  default-autowire="byName" default-lazy-init="true">
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="/WEB-INF/jdbc.properties" />
	</bean>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.hrbourse.application.model</value>
				<value>com.hrbourse.hr.model</value>
				<value>com.hrbourse.sys.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					<!-- Oracle org.hibernate.dialect.Oracle10gDialect -->
					 <!-- MySql--> org.hibernate.dialect.MySQLDialect 
				</prop>
			<!--<prop key="hibernate.hbm2ddl.auto">create</prop> -->
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.format_sql">false</prop>
				<prop key="hibernate.jdbc.batch_size">30</prop>
				<prop key="hibernate.default_batch_fetch_size">30</prop>
				<prop key="hibernate.query.substitutions">true 1, false 0</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="hibernate.cache.configurationResourceName">/WEB-INF/ehcache.xml</prop>
			</props>
		</property>
	</bean>
	<!--Hibernate TransactionManager -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事务增强切面 将com.feiling.dao下以Dao结尾的类纳入事务管理-->
	<aop:config>
		<aop:pointcut id="daoMethod" expression="execution(* com.hrbourse.*.dao.*(..))"/>
		<aop:pointcut id="utilMethod" expression="execution(* com.hrbourse.*.util.*(..))"/>
		<aop:advisor pointcut-ref="daoMethod" advice-ref="txAdvice"/>
		<aop:advisor pointcut-ref="utilMethod" advice-ref="txAdvice"/>
	</aop:config>
	<!-- 事务增强切面END -->
	<!-- 事务增强 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="add*" rollback-for="Exception"/>
			<tx:method name="update*"/>
			<tx:method name="remove*"/>
			<tx:method name="save*"/>
		</tx:attributes>
	</tx:advice>
	<!-- 事务增强END -->
<!--	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">-->
<!--		<property name="sessionFactory" ref="sessionFactory"></property>-->
<!--	</bean>-->
    <context:component-scan base-package="com.hrbourse.*" /><!-- 需要扫描的基类包,Spring容器将会扫描这个基类包里的所有类,并从类的注解信息获得 -->
</beans>

 先从配置文件加载数据库连接需要的东西,创建数据集datasource 对象,该对象又被sessionFactory对象依赖,sessionFactory对象中的<property name="packagesToScan">是指需要扫描的hibernate 注解的持久化类,这些包下的类都是用@注解了的持久化对象,这些对象是使用Criteria 查询,或者调用save,merg等方法时所需要的,没有这些注解你就不能调用serssion的save等方法,当然这些注解跟spring注解无关,spring注解是注入依赖所需要的标识(扯远了)。然后sessionFactory中再加入更过的配置入数据连接池以及二级缓存的配置(使用二级缓存书上说需要激活如query.setCacheable(true))。然后再说明下jpa这个概念 java ,persistent api,是sun公司提供的orm规范化接口。   

原文地址:https://www.cnblogs.com/wq123/p/3171966.html