java - How to write the Criteria queries dynamically? -


hi writing criteria query fetch employees. using generic type parameter method. user can pass class dynamically class want. employee class want add restriction dynamically if employee true want fetch record, otherwise record should not fetch. if user gives record without restriction, has fetch records.

public static <t> list getrowcount(class<t> classname) {      session ses = hibernateutil.getsessionfactory().opensession();     system.out.println("classname" + classname);     list<setpaginationrecords> s1 = new arraylist<setpaginationrecords>();     try {         criteria crit1 = ses.createcriteria(classname);         crit1.setprojection(projections.rowcount());         list l1 = crit1.list();         iterator it1 = l1.iterator();         if (it1.hasnext()) {             object o = it1.next();             totalnumberofrecords = integer.parseint(o.tostring());         }     } } 

this calling method.

 list<setpaginationrecords> paginationrecords = paginationclass.getrowcount(employeeentity.class);         request.setattribute("paginationrecords", paginationrecords); 

you may try (which example, outline how like):

session session = hibernateutil.getsessionfactory().opensession(); criteria criteria = session.createcriteria(employee.class); if (userchoice.femalesonly()) {     criteria.add(restrictions.eq("gender", gender.female)); } list<employee> employees = criteria.list(); 

here create criteria first, has no restrictions. means records fetched. check user data, , dependent on information given, augment criteria. , last, call list() fetch result.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -