CatDAOImpl and DogDAOImpl

Figure 1. One table for the whole hierarchy - DAO implementations for the concrete classes
package curam.inheritance;

import java.util.Set;

import com.google.inject.Singleton;

import curam.inheritance.Cat;
import curam.inheritance.CatDAO;
import curam.inheritance.struct.AnimalDtls;
import curam.test.codetable.ANIMAL_TYPE;
import curam.util.persistence.StandardDAOImpl;

@Singleton
public class CatDAOImpl extends StandardDAOImpl<Cat, AnimalDtls>
    implements CatDAO {
  private static final AnimalAdapter adapter = new AnimalAdapter();

  /**
   * Protected no-arg constructor for use only by Guice
   */
  protected CatDAOImpl() {
    super(adapter, Cat.class);
  }

  public Set<Cat> readAllCats() {
    return newSet(adapter.searchByAnimalType(ANIMAL_TYPE.CAT));
  }

}
package curam.inheritance;

import java.util.Set;

import com.google.inject.Singleton;

import curam.inheritance.struct.AnimalDtls;
import curam.test.codetable.ANIMAL_TYPE;
import curam.util.persistence.StandardDAOImpl;

@Singleton
public class DogDAOImpl extends StandardDAOImpl<Dog, AnimalDtls>
    implements DogDAO {
  private static final AnimalAdapter adapter = new AnimalAdapter();

  /**
   * Protected no-arg constructor for use only by Guice
   */
  protected DogDAOImpl() {
    super(adapter, Dog.class);
  }

  public Set<Dog> readAllDogs() {
    return newSet(adapter.searchByAnimalType(ANIMAL_TYPE.DOG));
  }

}

The DAO classes for the concrete classes are straightforward DAO implementations.

CatDAOImpl and DogDAOImpl each support the creation of new instances of their respective entities, as well as retrieval of existing instances, by making use of the StandardDAOImpl class (parameterized with AnimalDtls, the single database table).

Note that the searches to retrieve e.g. all Cat instances make use of a modeled searchByAnimalType method, as there is no Cat table from which to retrieve all rows. All searches performed by CatDAOImpl should ensure that they return only Cat rows, otherwise an error will be thrown from CatImpl.setEntityInfo.