hibernate重要知识点总结
一、使用注解方式-----實體和表之間的映射
配置spring的applicationContext.xml文件:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="cn.com.entry*"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
實體類的注解:
? ?1、注解實體與表的映射,在定義類的上方
? ? ? ? @Entity
? ? ? ? @Table
? ? ? ? @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)//這個是二級緩存的注解
? 2、注解主鍵,在定義屬性的上方
? ? ? ? @Id
? ? ? ? @GenericGenerator(name="inc50",strategy="increment")
? ? ? ? @GeneratedValue(generator="inc50")
二、開啟二級緩存
配置hibernate.cfg.xml文件
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
實體類注解
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)//這個是二級緩存的注解
三、連接池的配置
配置hibernate.cfg.xml文件
<property name="hibernate.c3p0.min_size">8</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.idle_test_period">30000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
四、事務管理
配置spring的applicationContext.xml文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd?">
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 注解方式 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
直接在要使用事務的方法上面或者類的上面注解,如下:
@Transactional(rollbackFor = Exception.class)//在類的上方注解表示整個類中的所有方法都使用事務管理。
注意:使用事務管理的方法中絕對不能catch異常,應該往外拋出異常,不然事務管理將失去作用
?
轉載于:https://www.cnblogs.com/wbjgogogo/p/7552268.html
總結
以上是生活随笔為你收集整理的hibernate重要知识点总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ROS学习笔记_创建工作空间(一)
- 下一篇: poj1018 Communicatio