我还有其他已经通用的BaseDaoImpl,但是我想将PaginationandSortiongRepository添加为通用,请帮助实现 我尝试了各种方法将GenericJPADAO添加为bean,但是不可能,是否还有其他实现方法?
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);
}}
无法添加GenericJPA,我不确定如何将Repository设为Generic, 提前致谢