New in Java - What are JPA and DAO?

I am new to Java and I am trying to create a web project with servlets. I would like to query my database, but I think I do not understand everything about JPA and DAO.

I was taught to do this:

  • Create class com.package.entity.User (generated from my database)
  • Create interface com.package.dao.UserDao
  • Create a class com.package.dao.jpa.JpaUserDao that implements UserDao
  • Create an EJB com.package.service.UserService using methods like public List<User> findAll()

I heard that there is no need to create a DAO interface with JPA, but I am completely lost and I donโ€™t understand what I should do or what EJB is. I just want to find all the users in my database and display their names in accordance with the practice of Java.

This is all normal for my servlets and JSPs.

What would you recommend?

+6
source share
2 answers

DAO means "Data Access Object". He abstracts the concept of "getting something from the data warehouse." Your DAO objects can be implemented using JDBC calls, JPA calls, or any other. Perhaps this calls some remote web service. Having DAO over JPA seems redundant and it adds a layer, but I think it's worth it.

For example, you might have the option of using "users with green eyes."

with direct JPA:

 List<User> users = entityManager.createQuery("select u from User u where u.EyeColor = 'green'""); 

with the DAO you have:

 List<User> users = dao.UsersWithEyeColor("green"); 

DAO has several advantages:

  • Easier to read.
  • It does not provide your database structure for the rest of the application.
  • It would be much easier for unit testing. A class that receives users with green eyes should only create a dao โ€œlayoutโ€. This is simpler than a mock JPA.

These are just a few of the arguments for using DAO. For a very simple, small application, this can be too much overhead. But for something that will become bigger and will need to be maintained over the years, I think it's worth it.

+7
source

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() { // Here I implement specific functionality to retrieve data using JPA Framework //EntityManagerFactory emf = ... //EntityManager em = ... //List<UserDTO> list = em.get...(); //return list; return null; } } 

For MySQL JDBC, you need to perform a different process:

 public class MySqlJDBCDaoFactory extends DAOFactory { @Override public List<UserDTO> listAllUsers() { //Connection = DriverManager ... //PreparedStatement ps = connection.prepareStatement("select * from ..."); //ResultSet = ps.executeQuery() // and so on... return null; } } 

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

+6
source

All Articles