Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] EclipseLink equivalent of Hibernate

Thanks for your reply Gordon.

In the last reply, I was trying to say with search by example coupled with
my domain model, its possible to design/build generic search + pagination +
sorting. As a result we can have one SearchService for UI. The new domain
model will abstract the Eclipse Link - ReadAllQuery + Expression +
Pagination + Sorting.

Say for example in UI we have 
FirstName  operator -------- Like                 (String)
LastName  operator -------- equal               (String)
RollNumber operator -------- greater than     (Number)
Salaray     operator --------  greater than     (Number)

Aim :- To create generic search for UI mentioned above

If my understanding of example policy is corrrect  ....
Saying policy.addSpecialOperation(String.class, "like") , will apply like on
FirstName,LastName
As result we will not get the desired result

Hence to overcome this problem I did the following object modeling

A SearchService accepts a SearchInput and returns SearchResult.
A searchInput object provides three kinds of information:
1.	Page Information (PageInput/PageOutput) - for pagination
2.	Sort information (SortCriteria) - for sorting
3.	Search filters information (any object is allowed here - a typical search
filter is a SearchModel. Useful subclasses exist for the SearchModel class
      SearchCriteria - capture search criteria from endpoint (UI) and
translate it to eclipse like expression
      (something similar to FilterFactory - used in eclipselink & OC
integration)

small eg

		ArrayList listIds = new ArrayList(1);
		listIds.add(3);
		GenericSearchCriteria<PasSegmentsDyn, Object, Object> gsc = new
GenericSearchCriteria<PasSegmentsDyn, Object, Object>();
		gsc.setPersistable(dyn);
		SearchCriteria searchCriteria = new SearchCriteria(
				new SearchExpressionGroup(new
SearchExpression("valueChar").like("Bought%")).
									  and(new
SearchExpression("dfusFieldUsageName").like("ACQUISIT%")))
			.or(new SearchExpressionGroup(new SearchExpression("id").in(listIds)))
			.or(new SearchExpressionGroup(new
SearchExpression("dfusFieldUsageName").eqIgnoreCase("Fnddate")));
		gsc.setSearchCriteria(searchCriteria);

		SearchInput searchInput = new SearchInput();
		searchInput.setSearchCriteria(gsc);
		searchService.search(searchInput);



Gordon Yorke-2 wrote:
> 
> I am not sure what you are asking now but the operators in the Example 
> Policy only apply to properties of certain types.  ie 
> policy.addSpecialOperation(String.class, "likeIgnoreCase"); only applies 
> to properties of type String.
> --Gordon
> 
> Gaurav Malhotra wrote:
>> Well this is interesting because the example policy get applied to all
>> the
>> properties in bean. In a normal search one want 'like' on string and so
>> on...
>> In order to solve the problem as follows :- (DDD approach)
>> GenericSearchCriteria :- A class capturing the search criteria
>> (encapsulate
>> operator like etc) & persistable entity. 
>> Eg
>> 		PasSegmentsDyn dyn = new PasSegmentsDyn(); // Persistable eclipselink
>> entity
>> 		dyn.setId(new Long(1));
>> 		GenericSearchCriteria<PasSegmentsDyn, Object, Object> gsc = new
>> GenericSearchCriteria<PasSegmentsDyn, Object, Object>();
>> 		gsc.setPersistable(dyn);
>> 		SearchCriteria searchCriteria = new SearchCriteria(
>> 				new SearchExpressionGroup(new
>> SearchExpression("valueChar").like("Bought%")).
>> 				and(new SearchExpression("dfusFieldUsageName").like("ACQUISIT%")));
>> 		gsc.setSearchCriteria(searchCriteria);
>>
>> 		SearchInput searchInput = new SearchInput();
>> 		searchInput.setSearchCriteria(gsc);
>> 		searchService.search(searchInput); // searchService is exposing search
>> service which in   
>>                                                                 // turn 
>> calls DAO layer
>>
>> GenerateEclipseLinkExpression :- Generate eclipse link expression reading
>> the Search criteria 
>>
>> DAO layer looks like
>> 				EntityManagerImpl emimpl =  (EntityManagerImpl) em.getDelegate();
>> 				Session session = emimpl.getActiveSession();
>> 				GenericSearchCriteria<T,Key,Value> gsc = (GenericSearchCriteria<T,
>> Key,
>> Value>) searchInput.getSearchCriteria();
>> 				SearchCriteria sc = gsc.getSearchCriteria();
>> 				
>> 				ReadAllQuery query = new ReadAllQuery();
>> 				query.setExampleObject(gsc.getPersistable());
>> 				GenerateEclipseLinkExpression genExpression = new
>> GenerateEclipseLinkExpression();
>> 			
>> query.setSelectionCriteria(genExpression.buildExpressionFromSearchCriteria(sc));
>> 				List results = (List) session.executeQuery(query);
>> 				
>> 				SearchResult searchResult = new SearchResult();
>> 				PageResult pageResult = new PageResult();
>> 				pageResult.setList(results);
>> 				searchResult.setPageResult(pageResult);
>> 				return results;
>>
>>
>>
>> Gordon Yorke-2 wrote:
>>   
>>> EclipseLink has Query By Example code but our options are configured 
>>> through a policy 
>>> (http://www.eclipse.org/eclipselink/api/1.0.1/org/eclipse/persistence/queries/QueryByExamplePolicy.html 
>>> ).
>>>
>>> An equivalent policy may look like:
>>>         QueryByExamplePolicy policy = new QueryByExamplePolicy();
>>>         policy.excludeValue(new Integer(0));
>>>         policy.addSpecialOperation(String.class, "likeIgnoreCase");
>>>         query.setQueryByExamplePolicy(policy);
>>>
>>> This policy can then be used on any QueryByExample query.  If you want 
>>> to control MatchMode you will need to update the string attributes of 
>>> the example to contain the pattern you want.
>>>
>>> You can create a JPA query from an EclipseLink query by using the EM 
>>> ((JPAEntityManager)em.getDelegate).createQuery(DatabaseQuery).
>>> --Gordon
>>>
>>>
>>> Gaurav Malhotra wrote:
>>>     
>>>> The below is my generic search written in hibernate.
>>>>    Question
>>>>      1. What is an equivalent of
>>>> .ignoreCase().excludeZeroes().enableLike(MatchMode.ANYWHERE); in
>>>> eclipselink. 
>>>>  
>>>>  public SearchResult loadAll(final SearchInput input) {
>>>>   final Persistable persistable = ((CrudSearchCriteria) input
>>>>     .getSearchCriteria()).getPersistable();
>>>>   logger.info("Persistable received is " + persistable);
>>>>   final SortCriteria sc = input.getSortCriteria();
>>>>   PageInput pi = input.getPageInput();
>>>>  
>>>>   List list = (List) getJpaTemplate().execute(new JpaCallback() {
>>>>    public Object doInJpa(javax.persistence.EntityManager em)
>>>>      throws javax.persistence.PersistenceException {
>>>>     final Example example = Example.create(persistable)
>>>>       .ignoreCase().excludeZeroes().enableLike(
>>>>         MatchMode.ANYWHERE);
>>>>     Criteria criteria = ((Session) em.getDelegate())
>>>>       .createCriteria(persistable.getClass()).add(example)
>>>>       .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
>>>>     Order order = null;
>>>>     if (sc.getName(0) != null)
>>>>      order = sc.isAscendingOrder(0) ? Order.asc(sc.getName(0))
>>>>        : Order.desc(sc.getName(0));
>>>>  
>>>>     if (order != null)
>>>>      criteria = criteria.addOrder(order);
>>>>  
>>>>     return criteria.list();
>>>>  
>>>>    }
>>>>  
>>>>   });
>>>>  
>>>>   SearchResult result = new SearchResult();
>>>>   PageResult pageResult = new PageResult();
>>>>  
>>>>   int rowsAvailable = list.size();
>>>>   pageResult.setRowsAvailable(rowsAvailable);
>>>>   result.setPageResult(pageResult);
>>>>   list = list.subList(pi.getStartRow(rowsAvailable), pi
>>>>     .getEndRow(rowsAvailable));
>>>>   pageResult.setList(list);
>>>>   return result;
>>>>  }
>>>>
>>>>
>>>>   
>>>>       
>>> _______________________________________________
>>> eclipselink-users mailing list
>>> eclipselink-users@xxxxxxxxxxx
>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>>
>>>
>>>     
>>
>>   
> 
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> 
> 

-- 
View this message in context: http://www.nabble.com/EclipseLink-equivalent-of-Hibernate-tp22509548p22573239.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top