« Return to Thread: org.hibernate.criterion.Example equivalent in Eclipselink

Re: org.hibernate.criterion.Example equivalent in Eclipselink

by Gaurav Malhotra :: Rate this Message:

Reply to Author | View in Thread

Thanks for the reply

I achieved this using

        public List<T> findByExample(T exampleCriteria) {
                final T t = exampleCriteria;
                List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
                        public Object doInJpa(javax.persistence.EntityManager em)
                                        throws javax.persistence.PersistenceException {
                                final ReadAllQuery query  = new ReadAllQuery(t.getClass());
                                query.useCollectionClass(LinkedList.class);
                                query .setExampleObject(t);
                                List<T> results = (List<T>) ((Session) em.getDelegate()).executeQuery(query);
                               
                                return results;
                        }
                });
               
                return list;
       
My Aim (using spring)
1. Write on Generic DAO
2. One CRUD service
3. One SearchService
4. Use generic to abstract over types.

JUnit testing is pending... will keep you posted.

Thanks...a bunch



Gaurav Malhotra wrote:
 In Hibernate I use to write the following generic code and utilize hibernates org.hibernate.criterion.Example.

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/Example.html

 public List<T> findByExample(T exampleCriteria) {
  final T t = exampleCriteria;
  List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
   public Object doInJpa(javax.persistence.EntityManager em)
     throws javax.persistence.PersistenceException {
    final Example example = Example.create(t).excludeZeroes();

    Criteria criteria = ((Session) em.getDelegate())
      .createCriteria(t.getClass()).add(example)
      .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    return criteria.list();
   }
  });
  return list;
 }

Whats an equivalent in EclipseLink??

 « Return to Thread: org.hibernate.criterion.Example equivalent in Eclipselink