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

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


    
  

Back to the top