criteria

更新时间:2022-11-22 16:48:43 阅读: 评论:0


2022年11月22日发(作者:苏州高博培训)

iaQueries

ngaCriteriainstance

ingtheresultt

ngtheresults

ations

cassociationfetching

equeries

tions,aggregationandgrouping

edqueriesandsubqueries

sbynaturalidentifier

Hibernatefeaturesanintuitive,extensiblecriteriaqueryAPI.

ngaCriteriainstance

sionisafactoryforCriteriainstances.

Criteriacrit=Criteria();

Results(50);

Listcats=();

ingtheresultt

ctionsdefines

factorymethodsforobtainingcertainbuilt-inCriteriontypes.

Listcats=Criteria()

.add(("name","Fritz%"))

.add(n("weight",minWeight,maxWeight))

.list();

Restrictionscanbegroupedlogically.

Listcats=Criteria()

.add(("name","Fritz%"))

.add((

("age",newInteger(0)),

("age")

))

.list();

Listcats=Criteria()

.add(("name",newString[]{"Fritz","Izi","Pk"}))

.add(ction()

.add(("age"))

.add(("age",newInteger(0)))

.add(("age",newInteger(1)))

.add(("age",newInteger(2)))

))

.list();

Therearearangeofbuilt-incriteriontypes(Restrictionssubclass).OneofthemostufulallowsyoutospecifySQLdirectly.

Listcats=Criteria()

.add(triction("lower({alias}.name)likelower(?)","Fritz%",))

.list();

The{alias}placeholderwithbereplacedbytherowaliasofthequeriedentity.

e():

Propertyage=e("age");

Listcats=Criteria()

.add(ction()

.add(())

.add((newInteger(0)))

.add((newInteger(1)))

.add((newInteger(2)))

))

.add(e("name").in(newString[]{"Fritz","Izi","Pk"}))

.list();

ngtheresults

.

Listcats=Criteria()

.add(("name","F%")

.addOrder(("name"))

.addOrder(("age"))

.tMaxResults(50)

.list();

Listcats=Criteria()

.add(e("name").like("F%"))

.addOrder(e("name").asc())

.addOrder(e("age").desc())

.tMaxResults(50)

.list();

ations

BynavigatingassociationsusingcreateCriteria()youcanspecifyconstraintsuponrelatedentities:

Listcats=Criteria()

.add(("name","F%"))

.createCriteria("kittens")

.add(("name","F%"))

.list();

ThecondcreateCriteria()returnsanewinstanceofCriteriathatreferstotheelementsofthekittenscollection.

Thereisalsoanalternateformthatisufulincertaincircumstances:

Listcats=Criteria()

.createAlias("kittens","kt")

.createAlias("mate","mt")

.add(erty("",""))

.list();

(createAlias()doesnotcreateanewinstanceofCriteria.)

ThekittenscollectionsheldbytheCatinstanceantto

retrievejustthekittensthatmatchthecriteria,youmustuaResultTransformer.

Listcats=Criteria()

.createCriteria("kittens","kt")

.add(("name","F%"))

.tResultTransformer(_TO_ENTITY_MAP)

.list();

Iteratoriter=or();

while(t()){

Mapmap=(Map)();

Catcat=(Cat)(_ALIAS);

Catkitten=(Cat)("kt");

}

cassociationfetching

YoucanspecifyassociationfetchingmanticsatruntimeusingtFetchMode().

Listcats=Criteria()

.add(("name","Fritz%"))

.tFetchMode("mate",)

.tFetchMode("kittens",)

.list();

tion19.1,“Fetchingstrategies”formoreinformation.

equeries

eallowsyoutoconstructaquerycriterionfromagiveninstance.

Catcat=newCat();

('F');

or();

Listresults=Criteria()

.add((cat))

.list();

Versionproperties,ult,nullvaluedpropertiesareexcluded.

YoucanadjusthowtheExampleisapplied.

Exampleexample=(cat)

.excludeZeroes()//excludezerovaluedproperties

.excludeProperty("color")//excludethepropertynamed"color"

.ignoreCa()//performcainnsitivestringcomparisons

.enableLike();//ulikeforstringcomparisons

Listresults=Criteria()

.add(example)

.list();

Youcanevenuexamplestoplacecriteriauponassociatedobjects.

Listresults=Criteria()

.add((cat))

.createCriteria("mate")

.add((e()))

.list();

tions,aggregationandgrouping

applyaprojectiontoaquerybycallingtProjection().

Listresults=Criteria()

.tProjection(nt())

.add(("color",))

.list();

Listresults=Criteria()

.tProjection(tionList()

.add(nt())

.add(("weight"))

.add(("weight"))

.add(roperty("color"))

)

.list();

Thereisnoexplicit"groupby"nprojectiontypesaredefinedtobegroupingprojections,whichalso

appearintheSQLgroupbyclau.

Analiascanbeassignedtoaprojectionetwodifferent

waystodothis:

Listresults=Criteria()

.tProjection((roperty("color"),"colr"))

.addOrder(("colr"))

.list();

Listresults=Criteria()

.tProjection(roperty("color").as("colr"))

.addOrder(("colr"))

.list();

Thealias()andas()methodssimplywrapaprojectioninstanceinanother,aliad,rtcut,youcanassignan

aliaswhenyouaddtheprojectiontoaprojectionlist:

Listresults=Criteria()

.tProjection(tionList()

.add(nt(),"catCountByColor")

.add(("weight"),"avgWeight")

.add(("weight"),"maxWeight")

.add(roperty("color"),"color")

)

.addOrder(("catCountByColor"))

.addOrder(("avgWeight"))

.list();

Listresults=Criteria(,"cat")

.createAlias("kittens","kit")

.tProjection(tionList()

.add(ty(""),"catName")

.add(ty(""),"kitName")

)

.addOrder(("catName"))

.addOrder(("kitName"))

.list();

e()toexpressprojections:

Listresults=Criteria()

.tProjection(e("name"))

.add(e("color").eq())

.list();

Listresults=Criteria()

.tProjection(tionList()

.add(nt().as("catCountByColor"))

.add(e("weight").avg().as("avgWeight"))

.add(e("weight").max().as("maxWeight"))

.add(e("color").group().as("color")

)

.addOrder(("catCountByColor"))

.addOrder(("avgWeight"))

.list();

edqueriesandsubqueries

TheDetachedCriteriaclassallowsyoutocreateaqueryoutsidethescopeofassionandthenexecuteitusinganarbitrarySession.

DetachedCriteriaquery=ss()

.add(e("x").eq('F'));

Sessionssion=....;

Transactiontxn=ransaction();

Listresults=cutableCriteria(ssion).tMaxResults(100).list();

();

();

ioninstancesinvolvingsubqueriescanbeobtainedviaSubqueriesor

Property.

DetachedCriteriaavgWeight=ss()

.tProjection(e("weight").avg());

Criteria()

.add(e("weight").gt(avgWeight))

.list();

DetachedCriteriaweights=ss()

.tProjection(e("weight"));

Criteria()

.add(("weight",weights))

.list();

Correlatedsubqueriesarealsopossible:

DetachedCriteriaavgWeightForSex=ss(,"cat2")

.tProjection(e("weight").avg())

.add(e("").eqProperty(""));

Criteria(,"cat")

.add(e("weight").gt(avgWeightForSex))

.list();

sbynaturalidentifier

Formostqueries,includingcriteriaqueries,thequerycachr,

thereisaspecialkindofquerywhereyoucanoptimizethecacheinvalidationalgorithm:

applications,teriaAPIprovidesspecialprovisionforthisuca.

First,mapthenaturalkeyofyourentityusingandenableuofthecond-levelcache.

Thisfunctionalityisnotintendedforuwithentitieswithmutablenaturalkeys.

OnceyouhaveenabledtheHibernatequerycache,lId()allowsyoutomakeuofthemoreefficientcachealgorithm.

Criteria()

.add(lId()

.t("name","gavin")

.t("org","hb")

).tCacheable(true)

.uniqueResult();

本文发布于:2022-11-22 16:48:43,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/90/358.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:willing
下一篇:algebra
标签:criteria
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图