org.hibernate.criterion.Example equivalent in Eclipselink

View: New views
6 Messages — Rating Filter:   Alert me  

org.hibernate.criterion.Example equivalent in Eclipselink

by Gaurav Malhotra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 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??

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

by tch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I believe the equivalent would be Expressions:

http://wiki.eclipse.org/Introduction_to_EclipseLink_Expressions_(ELUG)

./tch



On Mon, Jul 28, 2008 at 8:08 AM, Gaurav Malhotra
<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??
> --
> View this message in context: http://www.nabble.com/org.hibernate.criterion.Example-equivalent-in-Eclipselink-tp18688959p18688959.html
> Sent from the EclipseLink - Users mailing list archive at Nabble.com.
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@...
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

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

by Gaurav Malhotra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I get the following messgae when I click on the link http://wiki.eclipse.org/Introduction_to_EclipseLink_Expressions_(ELUG)


There is currently no text in this page, you can search for this page title in other pages or edit this page.

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??

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

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The link was missing the last ")",

http://wiki.eclipse.org/Introduction_to_EclipseLink_Expressions_(ELUG)

Expressions and possibly QueryByExample sounds what you are looking for.


I get the following messgae when I click on the link http://wiki.eclipse.org/Introduction_to_EclipseLink_Expressions_(ELUG)


There is currently no text in this page, you can search for this page title in other pages or edit this page.

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??


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

by Gaurav Malhotra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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??

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

by tch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here are some snippets that might be useful to you, good luck on your quest!

Templated selectAll:
        public <T> List<T> selectAll(Class<T> clazz, String orderClause) {
                if (orderClause.contains(",")) {
                        orderClause = orderClause.replaceAll("\\s", "");
                        String[] orderbys = orderClause.split(",");
                        orderClause = "";
                        for (int i=0;i<orderbys.length;i++) {
                                orderClause += "em." + orderbys[i];
                                if (i < orderbys.length - 1)
                                        orderClause += ", ";
                        }
                } else {
                        orderClause = "em." + orderClause;
                }
                Query query = getEntityManager().createQuery("SELECT em FROM " +
getClassName(clazz) + " em ORDER BY " + orderClause);

                return query.getResultList();
        }



Find Fresh: Guarantees you're getting the freshest entity out of the
DB (useful for crud)

    public <T> T findFresh(Class<T> entityClass, Object primaryKey) {
    T result =  getEntityManager().find(entityClass, primaryKey);
    if(result == null)
    return null;

    getEntityManager().refresh(result);
    return result;
    }



Get a named query results:
        public <T> List<T> getNamedQuery(Class<T> entityClass, String
name,Object...binds)
                {
                        Query query = getEntityManager().createNamedQuery(name);


                        for (int i = 0; i < binds.length; i++) {
                                Object tmp = binds[i];
                                query.setParameter(i+1,tmp);
                        }

                    return (List<T>) getNamedQuery(name,binds);
                }

./tch



On Mon, Jul 28, 2008 at 10:28 AM, Gaurav Malhotra
<gaurav.malhotra@...> wrote:

>
> 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??
>>
>
> --
> View this message in context: http://www.nabble.com/org.hibernate.criterion.Example-equivalent-in-Eclipselink-tp18688959p18689235.html
> Sent from the EclipseLink - Users mailing list archive at Nabble.com.
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@...
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users