-2

I'm trying to create an abstract class, to avoid bloating code for each DAO class.

public abstract GenericDAO<T> {
    public Optional<T> fetch(Long id) {
        return entityManager.find(T, id)
    }
}

I have tried the previous, but neither T nor T.class works. My goal is to simply have other DAOs extend this class.

How do i make this work? As in, how do i pass the wildcard as parameter to .find?

Edit:

To better illustrate.

I have DAO for each entity. Lets say ADao, BDao. The only difference between ADao and BDao is a the moment the fact, that they each accept and return different type. The JPA, however, doesn't care. Thus I want to create AbstractDao<T>, and have the children be ADao<EntityA> and BDao<EntityB>.

Thus, ADao nor BDao will include the repeated code, but each will work on the specific entity classes EntityA and EntityB (respectively).

Zerg Overmind
  • 955
  • 2
  • 14
  • 28

1 Answers1

-1

Maybe this is what you are looking for? Still not sure about your goal.

abstract class GenericDAO<T> {

    public abstract Optional<T> fetch(Long id);
}

class DaoChild extends GenericDAO<YourClass>{

   @Override
   public Optional<YourClass> fetch(Long id) {
      return entityManager.find(YourClass.class,id);
   }
}