Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Spring Users Unite!

Here is the GenericDAO 

Note:- CrudService is simple..just inject the below GenericDAO
          Difficult to share the pagination framework. But spring and
eclipselink make the things easier.

         Suggestion:- Think of abstracting
Search/SortCriteria/PageInput(since of pagination) offcourse Pageoutput and
use PageIterator design pattern

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.eclipse.persistence.internal.jpa.EntityManagerImpl;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;
import org.eclipse.persistence.sessions.Session;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.util.Assert;

import com.oracle.healthinsurance.model.entity.Persistable; // all entities
extends the interface persistable

public class GenericDAOJPAImpl<T extends Persistable> extends JpaDaoSupport
		implements GenericDAO<T> {
	
	
	public void create(final T t) {
		Object retVal=  getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(EntityManager em)
					throws PersistenceException {
				em.persist(t);
				return null;
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public T update(final T t) {
		//return getJpaTemplate().merge(t);
		T retVal=  (T) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(final EntityManager em)
					throws PersistenceException {
				T updatedValue = em.merge(t);
				return updatedValue;
			}
		});
		
		return retVal;
	}
	

	@SuppressWarnings("unchecked")
	public void delete(Class clas, Serializable id) {
		getJpaTemplate().remove(get(clas, id));
	}

	public void delete(T t) {
		getJpaTemplate().remove(t);
	}

	/**
	 * Find in memory object
	 * 
	 * @param T
	 *            to be searched object
	 */
	@SuppressWarnings("unchecked")
	public List<T> findByExample(final T exampleObject) {
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(EntityManager em)
					throws PersistenceException {
				final ReadAllQuery query = new ReadAllQuery(exampleObject.getClass());
				query.useCollectionClass(LinkedList.class);
				query.setExampleObject(exampleObject);
				EntityManagerImpl emimpl =  (EntityManagerImpl) em.getDelegate();
				Session session = emimpl.getActiveSession();
				List<T> results = (List<T>) session.executeQuery(query);
				
				return results;
			}
		});

		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<T> findByExample(final ReadAllQuery query) {
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(EntityManager em)
					throws PersistenceException {
				EntityManagerImpl emimpl =  (EntityManagerImpl) em.getDelegate();
				Session session = emimpl.getActiveSession();
				List<T> results = (List<T>) session.executeQuery(query);
				
				return results;
			}
		});

		return list;
	}


	@SuppressWarnings( { "unchecked", "hiding" })
	public <T> List<T> selectAll(final Class<T> clazz, String orderClause) {
		Assert.notNull(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 if (orderClause.trim().length() > 0) {
			orderClause = "em." + orderClause;
		}

		final String ordClause = orderClause;
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(final EntityManager em)
					throws javax.persistence.PersistenceException {
				StringBuilder queryStr = new StringBuilder();
				queryStr.append("SELECT em FROM ");
				queryStr.append(clazz.getSimpleName());
				queryStr.append(" em ");
				if (ordClause.length() > 0) {
					queryStr.append(" em ORDER BY ");
				}
				
				logger.debug(queryStr.toString());
				
				final Query query = em.createQuery(queryStr.toString());
				return query.getResultList();
			}
		});

		return list;
	}

	@SuppressWarnings( { "hiding", "unchecked" })
	public <T> T findFresh(final Class<T> entityClass, final Object primaryKey)
{
		T retVal = (T) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(final EntityManager em)
					throws javax.persistence.PersistenceException {
				T result = em.find(entityClass, primaryKey);
				
				if (result == null) {
					return null;
				}
				em.refresh(result);

				return result;
			}
		});

		return retVal;
	}

	// Revisit this method. Do not test it.
	// <TODO>
	@SuppressWarnings({ "hiding", "unchecked" })
	public <T> List<T> getNamedQuery(final Class<T> entityClass, final String
namedQuery,
			final Object... binds) {
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(final EntityManager em)
					throws javax.persistence.PersistenceException {
				Query query = em.createNamedQuery(namedQuery);
				
				for (int i = 0; i < binds.length; i++) {
					Object temp = binds[i];
					query.setParameter(i+1, temp);
				}
				
				return query.getResultList();
			}
		});		
		
		return list;
	}

	@SuppressWarnings({ "hiding", "unchecked" })
	public <T> List<T> getNamedQuery(final Class<T> entityClass, final String
namedQuery,
			final Map<String,Object> binds) {
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(final EntityManager em)
					throws PersistenceException {
				Query query = em.createNamedQuery(namedQuery);
				Set<String> bindKeys = binds.keySet();
				for (String key : bindKeys) {
					Object val = binds.get(key);
					query.setParameter(key, val);
				}
				
				return query.getResultList();
			}
		});		
		
		return list;
	}

	@SuppressWarnings({ "hiding", "unchecked" })
	public <T> List<T> getNamedQuery(final Class<T> entityClass, final String
namedQuery,
			final Map<String,Object> binds, final Map<String, Object> hints) {
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(final EntityManager em)
					throws PersistenceException {
				Query query = em.createNamedQuery(namedQuery);
				Set<String> bindKeys = binds.keySet();
				for (String key : bindKeys) {
					Object val = binds.get(key);
					query.setParameter(key, val);
				}
				Set<String> hintKeys = hints.keySet();
				for (String key : hintKeys) {
					Object val = hints.get(key);
					query.setHint(key, val);
				}
				
				return query.getResultList();
			}
		});		
		
		return list;
	}

	
	@SuppressWarnings("unchecked")
	public T get(Class clas, Serializable id) {
		return (T) getJpaTemplate().find(clas, id);
	}

	@SuppressWarnings({ "hiding", "unchecked" })
	public <T> List<T> search(final ReadAllQuery readAllQuery) {
		List<T> list = (List<T>) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(EntityManager em)
					throws PersistenceException {
				EntityManagerImpl emimpl =  (EntityManagerImpl) em.getDelegate();
				Session session = emimpl.getActiveSession();
				List<T> results = (List<T>) session.executeQuery(readAllQuery);
				
				return results;
			}
		});
		return list;
	}
}

Tim Hollosy wrote:
> 
> I've noticed on the list some people here - Gaurav Malhotra, etc. had
> a goal of using EclipseLink with Spring. How is this going for
> everyone? I was previously using Eclipselink in an RCP/OSGi
> environment but recently switched over to Spring (Web MVC) and am
> probably going through some of  the same growing pains:
> 
> Things I'm interested in:
> 
> -Your Generic DAO
> -Generic annotation based CRUD controller?
> -DisplayTag's PaginatedList implementation?
> 
> Anyone up for some code share here? I started implementing
> PaginatedList but ran into some problems due to the lack of a way to
> programatically set an order by clause via a property name. I have
> some ideas, but would rather not re-invent the wheel...
> 
> ./tch
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> 
> 

-- 
View this message in context: http://www.nabble.com/Spring-Users-Unite%21-tp19919618p19988656.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top