Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a DAO for join tables?

Tags:

join

dao

I'm currently on learning on using Dao pattern in my project. I know, one Table is equivalent to one Dao, am I right? just like StudentDao, SubjectDao.

Each Dao performs CRUD operations in their associated tables, but my question is, how am I going to create a DAO for joined tables? lets say I have a query to join student and subject table, then how do I create a DAOfor that?

Should I place it to the StudentDao? or to SubjectDao? or there's a good practice in that kind of situation?

like image 239
Jc dev Avatar asked May 29 '12 06:05

Jc dev


People also ask

What is DAO in JDBC?

ssss. 5. Dao clases are used to reuse the jdbc logic & Dao(Data Access Object) is a design pattern. dao is a simple java class which contains JDBC logic . Data Access Layer has proven good in separate business logic layer and persistent layer.

What is DAO method?

The Data Access Object (or DAO) pattern: separates a data resource's client interface from its data access mechanisms. adapts a specific data resource's access API to a generic client interface.

What is DAO in REST API?

The Data Access Object (DAO) pattern is a structural pattern that allows us to isolate the application/business layer from the persistence layer (usually a relational database but could be any other persistence mechanism) using an abstract API.

Can a DAO use another DAO?

You can do it, but that doesn't mean you should. In these cases, I like to use a Service ( CustomerService in this case) that has a method call that uses both DAOs. You can define the transaction around the service method, so if one call fails, they both roll back.


1 Answers

DAO - Data Access Object is Object that should only communicate with database. So if you want to JOIN two tables so you must have in your DTO Object StudentDTO reference on SubjectDTO.

public class StudentDTO {

   private String name;
   private String surname;
   private String age;
   private SubjectDTO subject;

  // getters, setters
}

So, SubjectDTO

public class SubjectDTO {

   private String name;
   private int room;

  // getters, setters
}

And DAO can look like this:

public StudentDAO {

   private final String SELECT_QUERY = "SELECT * FROM Student S JOIN Subject Sb ON (S.id = Sb.id)"

   public ArrayList<StudentDTO> getData() {

      ArrayList<StudentDTO> data = null;
      StudentDTO member = null;
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;

      try {
         con = OracleDAOFactory.getConnection();
         ps = con.prepareStatement(SELECT_QUERY);
         rs = ps.executeQuery();
         while (rs.next()) {
            member = new StudentDTO();
            member.setName(rs.getString(1));
            ...
            data.add(member);
         }
         return data;
      }
      catch (SQLException ex) {
         // body
      }
      finally {
         if (con != null) {
            con.close();
         }
      }
   }
}

I recommend to you check some tutorials.

Regards

like image 51
Simon Dorociak Avatar answered Sep 27 '22 21:09

Simon Dorociak