Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caseinsensitive Search Hibernate API

 
aadhar sharma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Could some one help me on

case insensitive search on the hibernate criteria API.

I am using the

criteria.addOrder(Order.asc("object"));

clause but this doesnt resolve my problem

Thanks and Regards

Aadhar Sharma

I am putting my query below



Criteria criteria = getCurrentSession().createCriteria(Material.class);
List searchRes = null;

try {

if (MaterialUtility.stringCheck(matSchDesc.getItemCode())) {

criteria.add(Expression.ilike("matlCd", matSchDesc
.getItemCode().trim()
+ "%"));
}

if (MaterialUtility.stringCheck(matSchDesc.getDescription())) {

criteria.add(Expression.ilike("itemDes", matSchDesc
.getDescription().trim()
+ "%"));
}

if (matSchDesc.getSelectedCategory() != null) {
if (matSchDesc.getSelectedCategory().getItemCategoryCd() != null
&& !MaterialUtility.stringTrim(
matSchDesc.getSelectedCategory()
.getItemCategoryCd()).equalsIgnoreCase(
"SELECT")) {
criteria.add(Expression.eq(
"matlCategory.id.itemCategoryCd", matSchDesc
.getSelectedCategory().getItemCategoryCd()
.trim()));

if (matSchDesc.getSelectedSubCategory() != null) {
if (matSchDesc.getSelectedSubCategory()
.getItemSubcatCd() != null
&& !MaterialUtility.stringTrim(
matSchDesc.getSelectedSubCategory()
.getItemSubcatCd())
.equalsIgnoreCase("SELECT")) {
criteria.add(Expression.eq(
"matlSubcat.id.itemSubcatCd", matSchDesc
.getSelectedSubCategory()
.getItemSubcatCd()));

if (matSchDesc.getSelectedItemType() != null) {
if (matSchDesc.getSelectedItemType()
.getItemTypeCd() != null
&& !MaterialUtility.stringTrim(
matSchDesc
.getSelectedItemType()
.getItemTypeCd())
.equalsIgnoreCase("SELECT")) {
criteria.add(Expression.eq(
"matlType.id.itemTypeCd",
matSchDesc.getSelectedItemType()
.getItemTypeCd().trim()));

}
}

}
}

}
}

if (matSchDesc.getSelectedCategoryType() != null) {
if (matSchDesc.getSelectedCategoryType().getCodeDes() != null
&& !MaterialUtility.stringTrim(
matSchDesc.getSelectedCategoryType()
.getCodeDes()).equalsIgnoreCase(
"SELECT")) {
criteria.add(Expression.ilike("itemCatTypCd", matSchDesc
.getSelectedCategoryType().getCodeDes().trim()
+ "%"));

}
}
if (MaterialUtility.stringCheck(matSchDesc.getGroupName())) {
criteria.add(Expression.ilike("groupNm", MaterialUtility
.stringTrim(matSchDesc.getGroupName())
+ "%"));

}
if (MaterialUtility.stringCheck(matSchDesc.getFinish())) {
criteria.add(Expression.ilike("itemFinishDes", MaterialUtility
.stringTrim(matSchDesc.getFinish())
+ "%"));

}
if (MaterialUtility.stringCheck(matSchDesc.getConstruction())) {
criteria.add(Expression.ilike("itemConstrDes", MaterialUtility
.stringTrim(matSchDesc.getConstruction())
+ "%"));

}
if (matSchDesc.getIsDutiable() != null
&& matSchDesc.getIsDutiable().booleanValue() != false) {
criteria.add(Expression.eq("isDutiableInd", matSchDesc
.getIsDutiable()));

}
boolean flag = true;

if (matSchDesc.getIsActiveInd() != null
&& matSchDesc.getIsActiveInd().booleanValue() != false) {

flag = false;

}
if (matSchDesc.getIsApprovedInd() != null
&& matSchDesc.getIsApprovedInd().booleanValue() != false) {
criteria.add(Expression.eq("isApprovedInd", matSchDesc
.getIsApprovedInd()));

}

if (matSchDesc.getShowMatDtlsInd() != null
&& matSchDesc.getShowMatDtlsInd().booleanValue() != false) {

boolean flagSelected;

flagSelected = false;

if (matSchDesc.getPrice().floatValue() > -1) {
flagSelected = true;
} else if (MaterialUtility.stringCheck(matSchDesc
.getWidthSize())
&& !MaterialUtility.stringTrim(
matSchDesc.getWidthSize()).equalsIgnoreCase("")) {
flagSelected = true;
} else if (MaterialUtility.stringCheck(matSchDesc
.getCorpInventoryCode())
&& !MaterialUtility.stringTrim(
matSchDesc.getCorpInventoryCode())
.equalsIgnoreCase("")) {
flagSelected = true;
} else if (MaterialUtility.stringCheck(matSchDesc
.getWeightStr())
&& MaterialUtility
.stringTrim(matSchDesc.getWeightStr())
.equalsIgnoreCase("")) {
flagSelected = true;
} else if (MaterialUtility.stringCheck(matSchDesc.getOrigin())
&& MaterialUtility.stringTrim(matSchDesc.getOrigin())
.equalsIgnoreCase("")) {
flagSelected = true;
} else if (MaterialUtility.stringCheck(matSchDesc
.getVendorMaterial())
&& MaterialUtility.stringTrim(
matSchDesc.getVendorMaterial())
.equalsIgnoreCase("")) {
flagSelected = true;
} else if (matSchDesc.getSelectedVendor() != null
&& matSchDesc.getSelectedVendor().getVendorId() != -1
&& matSchDesc.getSelectedVendor().getVendorId() > 0) {
flagSelected = true;
} else if (matSchDesc.getSelectedYear() != null
&& matSchDesc.getSelectedYear().getSeasonYear() != null
&& !MaterialUtility.stringTrim(
matSchDesc.getSelectedYear().getSeasonYear())
.equalsIgnoreCase("SELECT")) {
flagSelected = true;
} else {
flagSelected = false;
}

if (flagSelected) {
criteria.createAlias("materialSkus", "materialSkus");
}

if (matSchDesc.getPrice().floatValue() > -1) {

criteria
.add(Expression.eq("materialSkus.priceAmt",
new BigDecimal(matSchDesc.getPrice()
.floatValue())));

}
if (MaterialUtility.stringCheck(matSchDesc.getWidthSize())) {

criteria.add(Expression.eq("materialSkus.matlWdt",
MaterialUtility.stringTrim(matSchDesc
.getWidthSize())));

}
if (MaterialUtility.stringCheck(matSchDesc
.getCorpInventoryCode())) {

criteria.add(Expression.ilike("materialSkus.corpInvCd",
MaterialUtility.stringTrim(matSchDesc
.getCorpInventoryCode())
+ "%"));

}
if (MaterialUtility.stringCheck(matSchDesc.getWeightStr())) {

criteria.add(Expression.eq("materialSkus.matlWgt",
new BigDecimal(matSchDesc.getWeightStr())));

}
if (MaterialUtility.stringCheck(matSchDesc.getOrigin())) {

criteria.add(Expression.ilike("materialSkus.matlOriginNm",
MaterialUtility.stringTrim(matSchDesc.getOrigin())
+ "%"));

}
if (MaterialUtility.stringCheck(matSchDesc.getVendorMaterial())) {

criteria.add(Expression.ilike("materialSkus.vendorMatlNm",
MaterialUtility.stringTrim(matSchDesc
.getVendorMaterial())
+ "%"));

}

if (matSchDesc.getSelectedVendor() != null) {
if (matSchDesc.getSelectedVendor().getVendorId() != -1
&& matSchDesc.getSelectedVendor().getVendorId() > 0) {

criteria.createAlias("materialSkus.vendor", "x");
criteria.add(Expression.eq("x.vendorId", new Integer(
matSchDesc.getSelectedVendor().getVendorId())));

}
}

if (matSchDesc.getSelectedYear() != null) {
if (matSchDesc.getSelectedYear().getSeasonYear() != null
&& !MaterialUtility.stringTrim(
matSchDesc.getSelectedYear()
.getSeasonYear()).equalsIgnoreCase(
"SELECT")) {
criteria.createAlias("materialSkus.strideYr", "year");

criteria.add(Expression.eq("year.seasonYr",
new Integer(matSchDesc.getSelectedYear()
.getSeasonYear().trim())));

if (matSchDesc.getSelectedSeason() != null
&& !MaterialUtility.stringTrim(
matSchDesc.getSelectedSeason()
.getSeasonDesc())
.equalsIgnoreCase("SELECT")) {
System.out
.println("matSchDesc.getSelectedSeason().getSeasonDesc() is this season "
+ matSchDesc.getSelectedSeason()
.getSeasonDesc());
if (matSchDesc.getSelectedSeason().getSeasonId()
.intValue() > 0) {
criteria.createAlias("materialSkus.season",
"season");
criteria.add(Expression.eq("season.seasonId",
matSchDesc.getSelectedSeason()
.getSeasonId()));

}
}
}
}

}

criteria.add(Expression.eq("division.divisionId", division_id));
if (flag) {
Boolean isActiveInd = new Boolean(true);
criteria.add(Expression.eq("isActiveInd", isActiveInd));
} else {
Boolean isActiveInd = new Boolean(false);
criteria.add(Expression.eq("isActiveInd", isActiveInd));
}

if (matSchDesc.getNoOFRecordsPerPage() != null) {

criteria.setFirstResult(matSchDesc.getNoOFRecordsPerPage()
.intValue()
* (matSchDesc.getNextPageNumber().intValue() - 1));
criteria.setMaxResults(matSchDesc.getNoOFRecordsPerPage()
.intValue());

System.out
.println("No Of Records Per Page IS"
+ matSchDesc.getNoOFRecordsPerPage().intValue());
System.out.println("First Result"
+ matSchDesc.getNoOFRecordsPerPage().intValue()
* (matSchDesc.getNextPageNumber().intValue() - 1));
}
criteria.addOrder(Order.asc("matlCd"));
searchRes = criteria.list();

} catch (NullPointerException e) {
e.printStackTrace();
} catch (QueryException e) {
e.printStackTrace();
}

return searchRes;
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
case insensitive search on the hibernate criteria API.

I am using the

criteria.addOrder(Order.asc("object"));

clause but this doesnt resolve my problem


OK, order is not a search but the sorting of your results.

there is a ignoreCase() method on queries which will make it case insensitive.

Mark
 
aadhar sharma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark

could you give me an example of how to use it

Thanks and Regards

Aadhar Sharma
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by aadhar sharma:
Thanks Mark

could you give me an example of how to use it

Thanks and Regards

Aadhar Sharma


Not off the top of my head, just check out www.jboss.org the Hibernate Wiki and Documentation pages on querying.

Mark
 
aadhar sharma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark

Problem Resolved
 
aadhar sharma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has resolved my problem

criteria.addOrder(Order.asc("object").ignoreCase());
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by aadhar sharma:
This has resolved my problem

criteria.addOrder(Order.asc("object").ignoreCase());


There you go, just a method on a query object, Query, Criteria.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic