您可以創建一個BaseEntitymanager它提供了基本的CRUD服務和相應的DAO具有延長BaseEntityManager基實現。
BaseEntityManager:
public interface EntityManagerBase<E, K> {
E create(E entity);
void delete(E entity);
E update(E entity);
}
EntityManagerBaseImpl
public abstract class EntityManagerBaseImpl<E, K> implements
EntityManagerBase<E, K> {
private EntityManager entityManager;
private TransactionManager txManager;
private Class<E> type;
public E create(E entity) {
entityManager.persist(entity);
entityManager.flush();
return entity;
}
MyDaoImpl:
public class MyDaoImpl extends EntityManagerBaseImpl<MyDBO, Long> implements MyDao {
//put your implementation of methods
}
由於每個JPA提供者實現的EntityManager與自己的階級,既然你說你不能擴展的實現,那麼你就不能這樣做。 –