//////////2016-12-18 ~ 2016-12-19///////////
int study_data(){
Hibernate條件查詢
Criteria/DetachedCriteria
? ? 繼承自CriteriaSpecification
Criteria
? ? 對象須由session創(chuàng)建,通常稱為在線查詢
? ? 支持FetchMode禀综,排序葵第,F(xiàn)lushMode和LockMode
DetachedCriteria
? ? 對象不需要session參與創(chuàng)建奇瘦,通常稱為離線查詢
? ? 支持FetchMode,排序
Criterion/Projection
? ? Croterion是Criteria的查詢條件
Criterion的主要實現(xiàn)包括
? ? Example - 通過一個給定實例,構建一個條件查詢
? ? Junction - conjunction/disjunction實例通過Restrictions
? ? ? ? Conjunction = and
? ? ? ? Disjunction = or
? ? SimpleExpression
? ? Projections主要是讓Criteria能夠進行報表查詢演痒,并可以實現(xiàn)分組
? ? Projections查詢返回的是查詢結構的數(shù)組對象,并不是完整的映射實體對象
Projection的主要實現(xiàn)包括
? ? Simoleprojection
? ? ProjectionList
? ? Property
應用實例:
? ? Example:
? ? ? ? Criteria.add(Example.create())
? ? Conjunction:
? ? ? ? Restrictions.conjunction().add()
? ? ? ? Restrictions,disjunction().add()
? ? SimpleExpression:
? ? ? ? criteria.add(new SimpleExpression(*parentId*,parentId,*=*))
? ? Projections:
? ? ? ? Projections.projectionList() - 產(chǎn)生projectionList實例
? ? ? ? Projections.Property("propertyName") - 產(chǎn)生屬性投影
? ? ? ? Projections.max(),min(),sum()... - 產(chǎn)生聚合函數(shù)
? ? ? ? Projections.groupProperty("propertyName") - ?對屬性進行group by
Restrictions
Restrictions.eq - 等于
Restrictions.allEq - 參數(shù)為map趋惨,對比多個key/value
Restrictions.gt - 大于
Restrictions.ge - 大于等于
Restrictions.lt - 小于
Restrictions.le - 小于等于
Restrictions.between - 對應SQL的between子句
Restrictions.like - 對應SQl的like子句
Restrictions.in - 對應SQl的in子句
Restrictions.and - and關系
Restrictions.or - or關系
Restrictions.isNull - 判斷屬性是否為空鸟顺,為空則返回true
Restrictions.isNotNull - 與isNull相反
Restrictions.sqlRestriction - SQl限定的查詢
緩存機制
為了降低應用程序對物理數(shù)據(jù)源訪問的頻次,從而體改應用程序的運行性能
一級緩存器虾,又稱為"Session的緩存"
默認開啟讯嫂,無法關閉
二級緩存,又稱為"SessionFactory的緩存"
可選緩存策略兆沙,默認不開啟欧芽,非線程安全
緩存訪問策略
? ? 執(zhí)行對象查詢時,受限查找一級緩存中是否存在對象葛圃,存在則返回
? ? 不存在則查詢二級緩存中是否存在對象千扔,存在則返回
? ? 不存在則生產(chǎn)SQL提交數(shù)據(jù)庫查詢
? ? 由數(shù)據(jù)庫返回內(nèi)容包裝成尸體對象,返回實體對象同時更新緩存
一級緩存
一級緩存管理
管理一級緩存的常用方法
? ? veit(Object object) - 將指定的持久化對象從一級緩存中清除库正,釋放對象所占用的內(nèi)存資源曲楚,指定對象從持久化狀態(tài)變?yōu)橥泄軤顟B(tài),從而成為游離對象
? ? clear() - 將一級緩存中的所有持久化對象清除褥符,釋放其占用的內(nèi)存資源
? ? contains(Obejct obj) - 判斷指定的對象是否存在與一級緩存中
? ? flush() - 刷新一級緩存區(qū)的內(nèi)容龙誊,使之與數(shù)據(jù)庫數(shù)據(jù)保持同步
二級緩存
適合采用二級緩存管理的數(shù)據(jù)
? ? 很少被修改的數(shù)據(jù)
? ? 不是很重要,允許偶爾出現(xiàn)并發(fā)的數(shù)據(jù)
? ? 不會被并發(fā)訪問的數(shù)據(jù)
? ? 常量數(shù)據(jù)
不適合采用二級緩存管理的數(shù)據(jù)
? ? 經(jīng)常被修改的數(shù)據(jù)
? ? 絕對不允許出現(xiàn)并發(fā)訪問的數(shù)據(jù)喷楣,如財務數(shù)據(jù)趟大,絕對不允許出現(xiàn)并發(fā)
? ? 與其他應用共享的數(shù)據(jù)
二級緩存配置(EhCache)
hibernate.conf.xml:
<!-- 設置開啟二級緩存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 設置二級緩存插件EhCache的Provider類 -->
<property name="hibetnate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<!-- 啟動"查詢緩存" -->
<property name="hibetnate.cache.use_query_cache">true</property>
ehcache.xml:
<ehcache>
? ? <!-- 緩存的硬盤的路徑 -->
? ? <diskStore path="C:\Users\tencent\workspace\HibernateDemo\cache" />
? ? <!-- 默認設置
? ? maxElementsInMemory : 在內(nèi)存中最大緩存的對象數(shù)據(jù)
? ? eternal ? ? ? ? ? ? ? ? ? ? ? ? ? ?: 緩存的對象是否永遠不變
? ? timeToIdleSeconds ? ? ? ?: 可以操作對象的時間
? ? timeToLiveSeconds ? ? ? : 緩存中對象的生命周期鹤树,時間到后查詢數(shù)據(jù)會從數(shù)據(jù)庫重新讀取
? ? overflowToDisk ? ? ? ? ? ? ? : 內(nèi)存滿了,是否要緩存在硬盤 -->
? ? <defaultCache maxElementsInMemory="200" eternal="false"
? ? timeToIdleSeconds="50" timeToLiveSeconds="60" overflowToDisk="true" />
? ? <!-- 指定緩存的對象下面出現(xiàn)的屬性覆蓋上面出現(xiàn)的护昧,沒出現(xiàn)的繼承上面的 -->
? ? <cache name="pojo.PlayerStatus" maxElementsInMemory="200" ? ? eternal="false"?timeToIdleSeconds="50" timeToLiveSeconds="60" overflowToDisk="true" />
</ehcache>
annotation:
@Cache(usage = CacheConcurrencyStrategy)
<class>
? ? ...
? ? <cache usage="read-writer" />
? ? ...
</class>
可選的二級緩存(并發(fā))策略
? ? NONE - 不配置緩存并發(fā)策略
? ? READ-ONLY - 只讀
? ? READ-WRITER - 讀寫
? ? NONSTRICT_READ_WRITER - 非嚴格讀寫
? ? TRANSACTIONAL - 事務性
Lifecycle(回調(diào))
Hibernate通過實現(xiàn)Lifecycle接口執(zhí)行回調(diào)魂迄,實體對象通過實現(xiàn)Lifecycle接口,就可在特定的持久化階段執(zhí)行特定的處理過程
Lifecycle接口方法惋耙,拋出CallbackException:
? ? onSave(Session session):在實體對象save/insert操作之前調(diào)用
? ? onUpdate(Session session):在實體對象update操作之前調(diào)用
? ? onDelete(Session session):在實體對象delete操作之前調(diào)用
? ? onLoad(Session session):在實體對象被加載操作之后調(diào)用
對POJO有侵入性
Interceptor(攔截器)
攔截器相關的接口和類
? ? org.hibernate.Interceptor - Interface
? ? org.hibernate.EmptyIntercept - Class
設置攔截后捣炬,相應的操作都會先穿過一層層相應的攔截器,讓攔截器執(zhí)行預處理或善后處理
}