Repositories and the generic save() method

The last part of the backend implementation of the home page is implementing repositories for each entity. We will need to create the following repositories:

  • com.taskagile.domain.model.board.BoardRepository
  • com.taskagile.domain.model.board.BoardMemberRepository
  • com.taskagile.domain.model.team.TeamRepository

These repositories, as well as UserRepository, all have a save() method to persist entities. The only difference between the save() methods of different repositories is the entity to be saved. It would be better that we create a generic save() method in HibernateSupport to eliminate the code duplication.

The following is the change to HibernateSupport:

abstract class HibernateSupport<T> {
...
public void save(T object) {
entityManager.persist(object);
entityManager.flush();
}
}

As you can see, we add a generic type, <T>, to the class and use it in the save() method.

The following is the Hibernate implementation of TeamRepository:

@Repository
public class HibernateTeamRepository extends HibernateSupport<Team> implements TeamRepository {
...
@Override
public List<Team> findTeamsByUserId(UserId userId) {
String sql =
" SELECT t.* FROM team t WHERE t.user_id = :userId " +
" UNION " +
" ( " +
" SELECT t.* FROM team t, board b, board_member bm " +
" WHERE t.id = b.team_id AND bm.board_id = b.id AND bm.user_id =
:userId " +
" ) ";
NativeQuery<Team> query = getSession().createNativeQuery(sql,
Team.class);

query.setParameter("userId", userId.value());
return query.list();
}
}

As you can see, it only needs to implement the findTeamsByUserId() method and inherit the save() method from HibernateSupport. Inside this findTeamsByUserId() method, we use a UNION operation to retrieve the teams that the user created, as well as those teams that the current user has access to because they joined boards. We use Hibernate's NativeQuery to execute the query.

The following is the Hibernate implementation of BoardRepository:

@Repository
public class HibernateBoardRepository extends HibernateSupport<Board> implements BoardRepository {
...
@Override
public List<Board> findBoardsByMembership(UserId userId) {
String sql = "SELECT b.* FROM board b LEFT JOIN board_member bm ON
b.id = bm.board_id WHERE bm.user_id = :userId";

NativeQuery<Board> query = getSession().createNativeQuery(sql,
Board.class);
query.setParameter("userId", userId.value());
return query.list();
}
}

As you can see, in the findBoardsByMembership() method, we need to do a LEFT JOIN operation between the board table and the board_member table because we didn't use the @ManyToMany annotation to build the relationship between the Board entity and User entity. Instead, we create the BoardMember entity to keep the board membership information. As mentioned earlier, with the BoardMember entity, we can extend this board membership in a future version.

We skip BoardMemberRepository here because it inherits the save() method from HibernateSupport and does not have any other methods.

The following is the commit record of the backend:

Figure 11.11: Implementing the home page backend commit
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.22.179