Last active
December 18, 2015 13:19
-
-
Save rrajendran/5789391 to your computer and use it in GitHub Desktop.
JPA Dao Layer with default implementation by an abstract class. This is something similar to CrudRepository in spring data jpa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <beans xmlns="http://www.springframework.org/schema/beans" | |
| xmlns:camel="http://camel.apache.org/schema/spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xmlns:context="http://www.springframework.org/schema/context" | |
| xmlns:tx="http://www.springframework.org/schema/tx" | |
| xsi:schemaLocation="http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd | |
| http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
| http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd | |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> | |
| <context:annotation-config /> | |
| <context:component-scan base-package="com.xyz" /> | |
| <context:property-placeholder location="classpath:com/boohoo/esb/properties/database.properties" /> | |
| <tx:annotation-driven transaction-manager="txManager" /> | |
| <!-- | |
| Enables interpretation of the @PersistenceUnit/@PersistenceContext | |
| annotations providing convenient access to EntityManagerFactory/EntityManager | |
| --> | |
| <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> | |
| <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | |
| <property name="persistenceUnitName" value="reportIncident" /> | |
| <property name="jpaVendorAdapter" ref="jpaAdapterMYSQL" /> | |
| <property name="dataSource" ref="dataSource" /> | |
| </bean> | |
| <!-- OpenJPA MySQL adapter --> | |
| <bean id="jpaAdapterMYSQL" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"> | |
| <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.MySQLDictionary" /> | |
| <property name="showSql" value="true" /> | |
| </bean> | |
| <!-- DataSource MYSQL --> | |
| <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> | |
| <property name="driverClassName" value="${driver.class.name}" /> | |
| <property name="url" value="${url}" /> | |
| <property name="username" value="${username}" /> | |
| <property name="password" value="${password}" /> | |
| </bean> | |
| <!-- TransactionManager is required --> | |
| <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | |
| <property name="entityManagerFactory" ref="entityManagerFactory" /> | |
| <property name="dataSource" ref="dataSource" /> | |
| </bean> | |
| </beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.Serializable; | |
| public interface BaseDao<T, ID extends Serializable> { | |
| /** | |
| * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the | |
| * entity instance completely. | |
| * | |
| * @param entity | |
| * @return the saved entity | |
| */ | |
| <S extends T> S save(S entity); | |
| /** | |
| * Saves all given entities. | |
| * | |
| * @param entities | |
| * @return the saved entities | |
| * @throws IllegalArgumentException in case the given entity is (@literal null}. | |
| */ | |
| <S extends T> Iterable<S> save(Iterable<S> entities); | |
| /** | |
| * Retrieves an entity by its id. | |
| * | |
| * @param id must not be {@literal null}. | |
| * @return the entity with the given id or {@literal null} if none found | |
| * @throws IllegalArgumentException if {@code id} is {@literal null} | |
| */ | |
| <S extends T> S findOne(ID id); | |
| /** | |
| * Returns whether an entity with the given id exists. | |
| * | |
| * @param id must not be {@literal null}. | |
| * @return true if an entity with the given id exists, {@literal false} otherwise | |
| * @throws IllegalArgumentException if {@code id} is {@literal null} | |
| */ | |
| boolean exists(ID id); | |
| /** | |
| * Returns all instances of the type. | |
| * | |
| * @return all entities | |
| */ | |
| Iterable<T> findAll(); | |
| /** | |
| * Returns all instances of the type with the given IDs. | |
| * | |
| * @param ids | |
| * @return | |
| */ | |
| Iterable<T> findAll(Iterable<ID> ids); | |
| /** | |
| * Returns the number of entities available. | |
| * | |
| * @return the number of entities | |
| */ | |
| long count(); | |
| /** | |
| * Deletes the entity with the given id. | |
| * | |
| * @param id must not be {@literal null}. | |
| * @throws IllegalArgumentException in case the given {@code id} is {@literal null} | |
| */ | |
| void delete(ID id); | |
| /** | |
| * Deletes a given entity. | |
| * | |
| * @param entity | |
| * @throws IllegalArgumentException in case the given entity is (@literal null}. | |
| */ | |
| void delete(T entity); | |
| /** | |
| * Deletes the given entities. | |
| * | |
| * @param entities | |
| * @throws IllegalArgumentException in case the given {@link Iterable} is (@literal null}. | |
| */ | |
| void delete(Iterable<? extends T> entities); | |
| /** | |
| * Deletes all entities managed by the repository. | |
| */ | |
| void deleteAll(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static org.junit.Assert.*; | |
| import org.apache.commons.lang.RandomStringUtils; | |
| import org.junit.After; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.test.context.ContextConfiguration; | |
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
| import com.xyz.model.StockDao; | |
| @RunWith(SpringJUnit4ClassRunner.class) | |
| @ContextConfiguration(locations="classpath:testApplicationContext.xml") | |
| public class StockDaoImplTest { | |
| @Autowired | |
| StockDao stockDao; | |
| @Test | |
| public void save(){ | |
| Stock s = new Stock(); | |
| s.setQuantity(Integer.parseInt(RandomStringUtils.randomNumeric(4))); | |
| s.setSku(RandomStringUtils.randomAlphabetic(10)); | |
| s.setUpdatedDateTime("2012-12-12"); | |
| Stock save = stockDao.save(s); | |
| assertNotNull(save.getId()); | |
| } | |
| @After | |
| public void tearDown(){ | |
| stockDao.deleteAll(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.reflect.ParameterizedType; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.PersistenceContext; | |
| import javax.persistence.Query; | |
| public abstract class JpaBaseDao<T, ID> implements BaseDao<T, Integer>{ | |
| protected Class<T> entityClass; | |
| @PersistenceContext | |
| protected EntityManager entityManager; | |
| public JpaBaseDao() { | |
| ParameterizedType genericSuperclass = (ParameterizedType) getClass() | |
| .getGenericSuperclass(); | |
| // extracting the entity class | |
| this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0]; | |
| } | |
| @Override | |
| public <S extends T> S save(S entity) { | |
| return entityManager.merge(entity); | |
| } | |
| @Override | |
| public long count() { | |
| Query query = entityManager.createQuery("select count(*) from " + entityClass.getName()); | |
| return query.executeUpdate(); | |
| } | |
| @Override | |
| public void delete(Integer id) { | |
| T findOne = findOne(id); | |
| if(findOne != null) | |
| delete(findOne); | |
| else{ | |
| } | |
| } | |
| @Override | |
| public void delete(Iterable<? extends T> entities) { | |
| for (T t : entities) { | |
| if(t != null) | |
| delete(t); | |
| } | |
| } | |
| @Override | |
| public void delete(T entity) { | |
| entityManager.remove(entity); | |
| } | |
| @Override | |
| public void deleteAll() { | |
| Query query = entityManager.createQuery("DELETE FROM " + entityClass.getName() + " d"); | |
| query.executeUpdate(); | |
| } | |
| @Override | |
| public boolean exists(Integer id) { | |
| return findOne(id) == null ? false : true; | |
| } | |
| @Override | |
| public Iterable<T> findAll() { | |
| Query query = entityManager.createQuery("SELECT A FROM " + entityClass.getName() + " A"); | |
| return (List<T>)query.getResultList(); | |
| } | |
| @Override | |
| public Iterable<T> findAll(Iterable<Integer> ids) { | |
| List<T> list = new ArrayList<T>(); | |
| for (Integer id : ids) { | |
| if(id != null) | |
| list.add(findOne(id)); | |
| else{ | |
| //TODO: Log it | |
| } | |
| } | |
| return list; | |
| } | |
| @Override | |
| public T findOne(Integer id) { | |
| return entityManager.find(entityClass, id); | |
| } | |
| @Override | |
| public <S extends T> Iterable<S> save(Iterable<S> entities) { | |
| List<S> list = new ArrayList<S>(); | |
| for (S s : entities) { | |
| entityManager.merge(s); | |
| } | |
| return list; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.boohoo.esb.database.test.model.SageMasterStock; | |
| public interface StockDao extends BaseDao<Stock, Integer> { | |
| public Integer updateStock(Stock stock); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Repository("stockDao") | |
| @Transactional | |
| public class StockDaoImpl extends JpaBaseDao<Stock, Integer> implements StockDao { | |
| public Integer updateSageStock(Stock stock) { | |
| // Implementation here | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment