2

I have other BaseDaoImpl which is already generic but i want to add PaginationandSortiongRepository as generic, Please Help to Implement I have tried every way to add GenericJPADAO as bean but its not possible is there, Is there any other way to implement?

public interface BaseDao<T, ID extends Serializable> {

    public List<T> findAll();

    public T findOne(final long id);

    public T update(T object);

    public T get(Long id);

    public void delete(T object);

    public void insert(T object);

    public boolean exists(Long id);

    T getByCondition(String fieldName, Object value);

    List<T> getALL();
}





public class BaseDaoImpl<T, ID extends Serializable> implements BaseDao<T, ID> {
    @PersistenceContext
    protected EntityManager entityManager;

    private Class<T> entityType;

    public BaseDaoImpl() {
        this.entityType = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];

    }

    @Override
    @Transactional
    public void insert(T entity) {
        entityManager.persist(entity);
    }

    @Transactional
    public List<T> findAll() {
        return entityManager.createQuery("from " + entityType.getName()).getResultList();
    }

    @Override
    @Transactional
    public T findOne(long id) {
        return entityManager.find(entityType, id);
    }

    @Override
    @Transactional
    public T update(T entity) {
        return entityManager.merge(entity);
    }

    @Override
    @Transactional
    public T get(Long id) {
        // TODO Auto-generated method stub
        return entityManager.find(entityType, id);
    }

    @Override
    @Transactional
    public void delete(T object) {
        entityManager.remove(object);

    }

    @Override
    @Transactional
    public boolean exists(Long id) {
        return entityManager.contains(id);
    }

    @Override
    @Transactional
    public T getByCondition(String fieldName, Object value) {
        System.out.println(entityType);
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<T> criteria = cb.createQuery(entityType);
        Root<T> member = criteria.from(entityType);
        criteria.select(member).where(cb.equal(member.get(fieldName), value + ""));
        List<T> results = entityManager.createQuery(criteria).getResultList();
        if (results.isEmpty()) {
            return null;
        } else {
            return (T) results.get(0);
        }

    }

    @Override
    public List<T> getALL() {
        return null;
    }}





@NoRepositoryBean
public interface GenericJpaDao<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {

    enter code here

}





@Repository
public class AccountDaoImpl extends BaseDaoImpl<Account, Long> implements AccountDao {
    /*  Not able to Inject If add @Repository on GenericJapDao, How to Implement As generic Same as BaseDaoImpl*/   
    GenericJpaDao< Account, Long> generiCJPADao;

    @Override
    public Account getAccount(String emailAddress) {
        return getByCondition("emailAddress", emailAddress);
    }

    @Override
    public void saveAccount(Account account) {
        insert(account);

    }}

Not able to add GenericJPA and i am not sure how to make Repository as Generic, Thnaks in Advance

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • May be helps https://stackoverflow.com/questions/33511537/how-to-implement-generic-jpa-repository-in-spring-boot-which-can-be-autowired – harp1814 Jun 24 '19 at 18:22
  • 4
    Why? Spring Data JPA already does all of that, why are you implementing this yourself? – M. Deinum Jun 25 '19 at 07:38
  • You tried what? You only need to declare an interface, Spring Data JPA will do ther rest. You are working around the framework. Delete your classes and do `public interface AccountDao extends JpaRepository {}` you don't need more that that. – M. Deinum Jun 25 '19 at 08:39

0 Answers0