Create an implementation for your entity DAO interface

You must create an implementation class for your entity DAO interface.

Create a class in the same package as your DAO interface, and name the class after your entity, suffixed with DAOImpl:

Figure 1. Creating a DAO implementation file
package curam.mypackage;

/**
 * Standard implementation of {@linkplain MyNewEntityDAO}.
 */
public class MyNewEntityDAOImpl {
}

Your DAO implementation must implement the DAO interface:

Figure 2. Implementing the entity DAO interface
public class MyNewEntityDAOImpl implements MyNewEntityDAO {

However, if you were to directly implement this interface, you would have to write a huge amount of "plumbing" code. A great deal of plumbing is supplied by StandardDAOImpl, so extend this class, supplying the entity API and the generated Dtls struct for the database table as type parameters:

Figure 3. Extending StandardDAOImpl
import curam.mypackage.struct.MyNewEntityDtls;
import curam.util.persistence.StandardDAOImpl;

/**
 * Standard implementation of {@linkplain MyNewEntityDAO}.
 */
public class MyNewEntityDAOImpl extends
    StandardDAOImpl<MyNewEntity, MyNewEntityDtls>
    implements MyNewEntityDAO {

Annotate the class with @Singleton:

Figure 4. Annotating the DAO implementation as a Singleton
@Singleton
public class MyNewEntityDAOImpl extends
    StandardDAOImpl<MyNewEntity, MyNewEntityDtls>
    implements MyNewEntityDAO {

Create a private static variable to hold an instance of your entity adapter:

Figure 5. Declaring a static member variable for the entity adapter
/**
   * Single instance of the entity adapter shared across all DAO
   * implementations.
   */
  private static MyNewEntityAdapter adapter =
    new MyNewEntityAdapter();

Create a protected constructor which passes the adapter and the class of the entity API to StandardDAOImpl:

Figure 6. Creating a protected constructor
/**
   * @see StandardDAOImpl
   */
  protected MyNewEntityDAOImpl() {
    super(adapter, MyNewEntity.class);
  }

Use the "Add unimplemented methods" feature in Eclipse to add in the methods you must implement:

Figure 7. Adding unimplemented methods
public MyNewEntity readByName(String name) {
    // TODO Auto-generated method stub
    return null;
  }

  public Set<MyNewEntity> searchByParent(
    MyParentEntity myParentEntity) {
    // TODO Auto-generated method stub
    return null;
  }

  public Set<MyNewEntity> searchByType(
    final MYNEWENTITYTYPEEntry type) {
    // TODO Auto-generated method stub
    return null;
  }

The implementation of the non-standard singleton readByName calls the adapter to return a Dtls struct (by reading the database), and passes this to a StandardDAOImpl method to create an instance of your entity interface:

Figure 8. Implementing a singleton read
/**
   * {@inheritDoc}
   */
  public MyNewEntity readByName(final String name) {
    return getEntity(adapter.readByName(name));
  }

The implementation of the readmulti searchByParent calls the adapter to return an array of Dtls structs (by reading the database), and passes this to a StandardDAOImpl method to create set of instances of your entity interface:

Figure 9. Implementing a search
/**
   * {@inheritDoc}
   */
  public Set<MyNewEntity> searchByParent(
    final MyParentEntity myParentEntity) {
    return newSet(adapter.searchByParent(myParentEntity.getID()));
  }

The implementation of the readmulti searchByType must translate from the codetable value supplied to the String representation stored on the database:

Figure 10. Implementing a search based on a codetable value
/**
   * {@inheritDoc}
   */
  public Set<MyNewEntity> searchByType(
    final MYNEWENTITYTYPEEntry type) {
    return newSet(adapter.searchByType(type.getCode()));
  }

Your implementation of the DAO interface is now complete. However, there is a final important step, which is to specify your DAO implementation as the default implementation of the DAO interface.

Open the DAO interface and add an annotation prescribing the default implementation:

Figure 11. Specifying the DAO implementation as the default implementation of the DAO interface
/**
 * Data access for {@linkplain MyNewEntity}.
 */
@ImplementedBy(MyNewEntityDAOImpl.class)
public interface MyNewEntityDAO extends StandardDAO<MyNewEntity> {

If you fail to do this step, then when your application runs you will likely see a NullPointerException when Guice fails to inject instances of your DAO interface:

Figure 12. Null pointer exceptions will occur if no default DAO implementation is specified on the DAO interface
/*
 * This variable will be null if you don't specify the default
 * implementation of MyNewEntityDAO properly...
 */
@Inject
private MyNewEntityDAO myNewEntityDAO;

Putting it all together

Here's the complete code for the DAO implementation:

Figure 13. Complete listing for an entity DAO implementation
package curam.mypackage;

import java.util.Set;

import com.google.inject.Singleton;

import curam.mypackage.struct.MyNewEntityDtls;
import curam.util.persistence.StandardDAOImpl;

/**
 * Standard implementation of {@linkplain MyNewEntityDAO}.
 */
@Singleton
public class MyNewEntityDAOImpl extends
    StandardDAOImpl<MyNewEntity, MyNewEntityDtls> implements
    MyNewEntityDAO {

  /**
   * Single instance of the entity adapter shared across all DAO
   * implementations.
   */
  private static MyNewEntityAdapter adapter =
      new MyNewEntityAdapter();

  /**
   * @see StandardDAOImpl
   */
  protected MyNewEntityDAOImpl() {
    super(adapter, MyNewEntity.class);
  }

  /**
   * {@inheritDoc}
   */
  public MyNewEntity readByName(final String name) {
    return getEntity(adapter.readByName(name));
  }

  /**
   * {@inheritDoc}
   */
  public Set<MyNewEntity> searchByParent(
      final MyParentEntity myParentEntity) {
    return newSet(adapter.searchByParent(myParentEntity.getID()));
  }

}