The Artima Developer Community
Sponsored Link

Java Answers Forum
Generic Query in hibernate

1 reply on 1 page. Most recent reply: Mar 8, 2009 11:44 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Sandeep Vaid

Posts: 1
Nickname: catchsandy
Registered: Mar, 2009

Generic Query in hibernate Posted: Mar 5, 2009 6:06 AM
Reply to this message Reply
Advertisement
public class Item {
private String name;
private Set categories = new HashSet();
private Set bids = new HashSet();
private Bid successfulBid;
...
}
----------------------------------------------------
public class Bid {
...
private Item item;
...
}

-----------------------------------------------------
public class User {
private Address shippingAddress;
private Set billingDetails;
..
}

-----------------------------------------------------
public class Category {
private String name;
private Category parentCategory;
private Set childCategories = new HashSet();
private Set items = new HashSet();
..
}
-----------------------------------------------------



Considering Item, Bid, User, Category:
The usecase requirement is as follows:

USECASE 1 : Require item.successfulBid, item.categories.parentCategory
USECASE 2 : Require item.categories.childCategories
USECASE 3 : Require item.bid , item.user.shippingAddress, item.user.billingDetails
USECASE 4 : Require complete item (including it's associations and collections populated)

If i write a generic DAO method which will accept which all populated associations and collections of Item should be written...
Based on this input (and dynamic eager fetching), i will create a dynamic HQL which will fetch only the required data.


Item getItem(String itemId, Vector criteria){
//generate dynamic HQL using this criteria elements in fetch clause
}


So in:
UseCase1:
Vector criteria = new Vector(0,1);
criteria.add("item.successfulBid");
criteria.add("item.categories.parentCategory");
Item item = getItem(criteria);

UseCase2:
Vector criteria = new Vector(0,1);
criteria.add("item.categories.childCategories");
Item item = getItem(criteria);

UseCase3:
Vector criteria = new Vector(0,1);
criteria.add("item.bid");
criteria.add("item.user.shippingAddress");
criteria.add("item.user.billingDetails");
Item item = getItem(criteria);

UseCase4:
Vector criteria = new Vector(0,1);
criteria.add("*");
Item item = getItem(criteria);

Is this a good idea..? Comments plz..


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Generic Query in hibernate Posted: Mar 8, 2009 11:44 PM
Reply to this message Reply
Please format your code... Not quite sure what you are asking but how about something to this affect:

class PropertyValueTuple {
   private String propertyName;
   private Object val;
 
   public PropertyValueTuple(String propertyName, val) {
      this.propertyName = propertyName;
      this.val = val;
   }
 
   // TODO: Generate Getters and Setters
 
}
 
class MyCriteriaBlahBlah {
 
   public Criteria createEqualsCriteria(PropertyValue ... criterias) {
      Criteria criteria = CriteriaFactory.buildCriteria(ClassA.class.getName());
      for(PropertyValue critValue : criterias) {
         criteria.add(Restrictions.equals(critValue.getPropertyName, critValue.getValue());
      }
   }
 
}
 
 



You can then use createEqualsCriteria to deal with all equals type stuff. Keep in mind I'm not 100% spot on on the criteria Building part as I usually do this once off and forget how to instantiate it. Good luck!!

Flat View: This topic has 1 reply on 1 page
Topic: JAX India 2009: International Conference on Java Technologies April 06-10, Previous Topic   Next Topic Topic: usb.core

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use