kk wayne
kk wayne

Reputation: 1

DAO Design Pattern for multiple tables

There are 2 existing DAO

  1. UniversityDAO.java
  2. StudentDAO.java

There a newly added DAO call ComputerDAO.java

Table Design

If there is a query as below then what will be the best design ?

Display all university with student name and computer name from the University module.

Expected Output or Sample University Module display

select * from university 
join student on uni_id = student_id
join computer on computer_id on uni_id

What is the best DAO Implementation? i.e new class or existing class implementation

  1. Implement the method getUniStudentCompName() on UniversityDAO.java
  2. Create another DAO call UniversityStudentDAO.java and implement the method getUniStudentCompName()

Upvotes: 0

Views: 159

Answers (1)

Choudhary
Choudhary

Reputation: 27

Looking at the ER diagram, the computer table is not related to the University but to the student table. If there is a student table then there is also a computer table.

For DB support you will be required to use some tools like JPA and hibernate. the relationship could be annotated with OneToOne on name fields.

There are multiple ways you can fetch the desired result. To get started you can look at:

  • implementing JPA data rest and using a repository interface with a custom query in it.
@Query("SELECT name FROM Student s join query ... WHERE u.status = ?1")
Student getStudentData(UUID id); 

If you want a list then something like:

@Query("SELECT name FROM Student s join query ...")
Collection<Student> findAll(); 
  • use springboot projection if you were to use Springboot, It a very easy and clean.

  • You can opt-in with a new Service Class name something whatever and use it to bootstrap all these entity classes. You can do like:

ServiceClass {
    UniversityRepository unirepo;
    StudentRepository studrepo;

    List getAllNames() {
      listName = [];
      // push your names to the blob!!!
      return listName;
    }

}

Upvotes: 0

Related Questions