加强hibernate的criteria查询中的使用Example查询Word文档格式.docx
- 文档编号:21649178
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:11
- 大小:18.74KB
加强hibernate的criteria查询中的使用Example查询Word文档格式.docx
《加强hibernate的criteria查询中的使用Example查询Word文档格式.docx》由会员分享,可在线阅读,更多相关《加强hibernate的criteria查询中的使用Example查询Word文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
}
//.....中间是分页等处理
List<
?
>
result
criteria.list();
[/code]
注意上面的注释,Example进行查询是会忽略所有的null值以及关联的对象,故,如果我想根据empoyee
的department来查询empoyee
的话,就会将所有的empoyee
都查询出来,因为department被忽略了。
遇到这个问题有两种解决方法:
一、我的做法如下:
//加强后的Example查寻,不再忽略关联对象
criteria.createCriteria("
dpartment"
).add(Restrictions.eq("
id"
empoyee
.getDepartment.getId()));
二、下面是我在网上看到别人的做法,个人感觉有点麻烦:
(转)那么这个问题怎么解决呢,查看了Example类的源码后,我决定动手修改——当然不能直接去改它的源码,在参考了hibernate官方论坛后,新建了类:
importjava.util.ArrayList;
importjava.util.HashSet;
importjava.util.List;
importjava.util.Set;
importorg.hibernate.Criteria;
importorg.hibernate.EntityMode;
importorg.hibernate.HibernateException;
importorg.hibernate.criterion.CriteriaQuery;
importorg.hibernate.criterion.Criterion;
importorg.hibernate.criterion.MatchMode;
importorg.hibernate.criterion.Restrictions;
importorg.hibernate.criterion.SimpleExpression;
importorg.hibernate.engine.TypedValue;
importorg.hibernate.persister.entity.EntityPersister;
importorg.hibernate.type.AbstractComponentType;
importorg.hibernate.type.Type;
importorg.hibernate.util.StringHelper;
/**
*AcopyofHibernate'
sExampleclass,withmodificationsthatallowyouto
*includemany-to-oneandone-to-oneassociationsinQueryByExample(QBE)
*queries.associationclass<
/a>
*/
publicclassAssociationExampleimplementsCriterion{
privatefinalObjectentity;
privatefinalSetexcludedProperties=newHashSet();
privatePropertySelectorselector;
privatebooleanisLikeEnabled;
privatebooleanisIgnoreCaseEnabled;
privateMatchModematchMode;
privatebooleanincludeAssociations=true;
/**
*Astrategyforchoosingpropertyvaluesforinclusioninthequery
*criteria
publicstaticinterfacePropertySelector{
publicbooleaninclude(ObjectpropertyValue,StringpropertyName,
Typetype);
}
privatestaticfinalPropertySelectorNOT_NULL=newNotNullPropertySelector();
privatestaticfinalPropertySelectorALL=newAllPropertySelector();
privatestaticfinalPropertySelectorNOT_NULL_OR_ZERO=newNotNullOrZeroPropertySelector();
staticfinalclassAllPropertySelectorimplementsPropertySelector{
publicbooleaninclude(Objectobject,StringpropertyName,Typetype){
returntrue;
}
staticfinalclassNotNullPropertySelectorimplementsPropertySelector{
returnobject!
=null;
staticfinalclassNotNullOrZeroPropertySelectorimplements
PropertySelector{
=null
&
&
(!
(objectinstanceofNumber)||((Number)object)
.longValue()!
=0);
*Setthepropertyselector
publicAssociationExamplesetPropertySelector(PropertySelectorselector){
this.selector=selector;
returnthis;
*Excludezero-valuedproperties
publicAssociationExampleexcludeZeroes(){
setPropertySelector(NOT_NULL_OR_ZERO);
*Don'
texcludenullorzero-valuedproperties
publicAssociationExampleexcludeNone(){
setPropertySelector(ALL);
*Usethe"
like"
operatorforallstring-valuedproperties
publicAssociationExampleenableLike(MatchModematchMode){
isLikeEnabled=true;
this.matchMode=matchMode;
publicAssociationExampleenableLike(){
returnenableLike(MatchMode.EXACT);
*Ignorecaseforallstring-valuedproperties
publicAssociationExampleignoreCase(){
isIgnoreCaseEnabled=true;
*Excludeaparticularnamedproperty
publicAssociationExampleexcludeProperty(Stringname){
excludedProperties.add(name);
*Createanewinstance,whichincludesallnon-nullpropertiesbydefault
*
*@paramentity
*@returnanewinstanceof<
tt>
Example<
/tt>
publicstaticAssociationExamplecreate(Objectentity){
if(entity==null)
thrownewNullPointerException("
nullAssociationExample"
);
returnnewAssociationExample(entity,NOT_NULL);
protectedAssociationExample(Objectentity,PropertySelectorselector){
this.entity=entity;
publicStringtoString(){
return"
example("
+entity+'
)'
;
privatebooleanisPropertyIncluded(Objectvalue,Stringname,Typetype){
return!
excludedProperties.contains(name)
&
selector.include(value,name,type)
type.isAssociationType()||(type.isAssociationType()
&
includeAssociations&
!
type.isCollectionType()));
publicStringtoSqlString(Criteriacriteria,CriteriaQuerycriteriaQuery)
throwsHibernateException{
StringBufferbuf=newStringBuffer().append('
('
EntityPersistermeta=criteriaQuery.getFactory().getEntityPersister(
criteriaQuery.getEntityName(criteria));
String[]propertyNames=meta.getPropertyNames();
Type[]propertyTypes=meta.getPropertyTypes();
//TODO:
getallproperties,notjustthefetchedones!
Object[]propertyValues=meta.getPropertyValues(entity,getEntityMode(
criteria,criteriaQuery));
for(inti=0;
i<
propertyNames.length;
i++){
ObjectpropertyValue=propertyValues[i];
StringpropertyName=propertyNames[i];
booleanisPropertyIncluded=i!
=meta.getVersionProperty()
isPropertyIncluded(propertyValue,propertyName,
propertyTypes[i]);
if(isPropertyIncluded){
if(propertyTypes[i].isComponentType()){
appendComponentCondition(propertyName,propertyValue,
(AbstractComponentType)propertyTypes[i],criteria,
criteriaQuery,buf);
}else{
appendPropertyCondition(propertyName,propertyValue,
criteria,criteriaQuery,buf);
}
}
if(buf.length()==1)
buf.append("
1=1"
//yuck!
returnbuf.append('
).toString();
privatestaticfinalObject[]TYPED_VALUES=newTypedValue[0];
publicTypedValue[]getTypedValues(Criteriacriteria,
CriteriaQuerycriteriaQuery)throwsHibernateException{
Object[]values=meta.getPropertyValues(entity,getEntityMode(
Listlist=newArrayList();
Objectvalue=values[i];
Typetype=propertyTypes[i];
Stringname=propertyNames[i];
isPropertyIncluded(value,name,type);
addComponentTypedValues(name,value,
(AbstractComponentType)type,list,criteria,
criteriaQuery);
addPropertyTypedValue(value,type,list);
return(TypedValue[])list.toArray(TYPED_VALUES);
privateEntityModegetEntityMode(Criteriacriteria,
CriteriaQuerycriteriaQuery){
EntityModeresult=meta.guessEntityMode(entity);
if(result==null){
thrownewClassCastException(entity.getClass().getName());
returnresult;
protectedvoidaddPropertyTypedValue(Objectvalue,Typetype,Listlist){
if(value!
=null){
if(valueinstanceofString){
Stringstring=(String)value;
if(isIgnoreCaseEnabled)
string=string.toLowerCase();
if(isLikeEnabled)
string=matchMode.toMatchString(string);
value=string;
list.add(newTypedValue(type,value,null));
protectedvoidaddComponentTypedValues(Stringpath,Objectcomponent,
AbstractComponentTypetype,Listlist,Criteriacriteria,
if(component!
String[]propertyNames=type.getPropertyNames();
Type[]subtypes=type.getSubtypes();
Object[]values=type.getPropertyValues(component,getEntityMode(
criteria,criteriaQuery));
for(inti=0;
Objectvalue=values[i];
Typesubtype=subtypes[i];
Stringsubpath=StringHelper.qualify(path,propertyNames[i]);
if(isPropertyIncluded(value,subpath,subtype)){
if(subtype.isComponentType()){
addComponentTypedValues(subpath,value,
(AbstractComponentType)subtype,list,
criteria,criteriaQuery);
}else{
addPropertyTypedValue(value,subtype,list);
}
protectedvoidappendPropertyCondition(StringpropertyName,
ObjectpropertyValue,Criteriacriteria,CriteriaQuerycq,
StringBufferbuf)throwsHibernateException{
Criterioncrit;
if(propertyValue!
booleanisString=propertyValueinstanceofString;
SimpleExpressionse=(isLikeEnabled&
isString)?
Restrictions
.like(proper
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 加强 hibernate criteria 查询 中的 使用 Example
![提示](https://static.bdocx.com/images/bang_tan.gif)