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:
package curam.mypackage; /** * Standard implementation of {@linkplain MyNewEntityDAO}. */ public class MyNewEntityDAOImpl { }
Your DAO implementation must implement the 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:
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:
@Singleton public class MyNewEntityDAOImpl extends StandardDAOImpl<MyNewEntity, MyNewEntityDtls> implements MyNewEntityDAO {
Create a private static variable to hold an instance of your 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:
/** * @see StandardDAOImpl */ protected MyNewEntityDAOImpl() { super(adapter, MyNewEntity.class); }
Use the "Add unimplemented methods" feature in Eclipse to add in the methods you must implement:
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:
/** * {@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:
/** * {@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:
/** * {@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:
/** * 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:
/* * This variable will be null if you don't specify the default * implementation of MyNewEntityDAO properly... */ @Inject private MyNewEntityDAO myNewEntityDAO;
Here's the complete code for the 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())); } }