DAO (Data Access Object) is basically a programming template, for this you have to create a class that will create an object that provides an abstract interface for some type of persistence unit (db, xml file system, etc.).). Why is this useful? Because it provides some specific data operations without revealing database details.
Basic DAO example:
import java.util.List; public abstract class DAOFactory { public static final int MYSQL_JDBC = 1; public static final int MYSQL_JPA = 2; public static final int MYSQL_HIBERNATE = 3; public abstract List<UserDTO> listAllUsers(); public static DAOFactory getDAOFactory(int whichFactory) { switch (whichFactory) { case MYSQL_JDBC : return new MySqlJDBCDaoFactory(); case MYSQL_JPA: return new MySqlJpaDaoFactory(); case MYSQL_HIBERNATE: return new MySqlHibernateDaoFactory(); default: return null; } } }
Then you need to create a specific factory for each type of persistence that you will manage in your application and that particular factory should implement the methods that you use to save, for example listAllUsers ();
For example, for MySQL JPA:
public class MySqlJpaDaoFactory extends DAOFactory { @Override public List<UserDTO> listAllUsers() {
For MySQL JDBC, you need to perform a different process:
public class MySqlJDBCDaoFactory extends DAOFactory { @Override public List<UserDTO> listAllUsers() {
Then you call your factory as follows:
DAOFactory myfactory = DAOFactory.getDAOFactory(DAOFactory.MYSQL_JDBC); List<UserDTO> list = myfactory.listAllUsers();
And if you see that if you change the database structure or persistence mode, you donโt need to reinvent the wheel, just change the parameter and you will get the persistence implementation that you want, just based on the parameter.
Hope this helps you understand the pattern, I do not use EJB, and if you use DAO, I do not think that it is still necessary to implement EJB.
Best wishes