Commit 82e2c611 authored by Marek Kadlečík's avatar Marek Kadlečík
Browse files

Added join fetch method to load matches with relational attributes

parent e6fd3bdc
Pipeline #144749 passed with stage
in 1 minute and 6 seconds
......@@ -2,6 +2,7 @@ package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.dto.MatchDTO;
import cz.muni.fi.pa165.dto.PlayerDTO;
import cz.muni.fi.pa165.entity.MatchEntity;
import java.util.List;
......@@ -12,6 +13,8 @@ public interface MatchFacade {
List<MatchDTO> findAllMatches();
List<MatchDTO> findAllMatchesWithPlayersAndTeams();
MatchDTO getMatchById(String uuid);
String createMatch(MatchDTO matchDTO);
......
......@@ -3,8 +3,11 @@ package cz.muni.fi.pa165.dao.match;
import cz.muni.fi.pa165.dao.BaseDao;
import cz.muni.fi.pa165.entity.MatchEntity;
import java.util.List;
/**
* @author Dominik Kozubík
*/
public interface MatchDao extends BaseDao<MatchEntity> {
List<MatchEntity> findAllWithPlayersAndTeams();
}
......@@ -6,16 +6,23 @@ import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
/**
* @author Dominik Kozubík
*/
@Repository
public class MatchDaoImpl extends BaseDaoImpl<MatchEntity> implements MatchDao {
// @PersistenceContext
// private EntityManager em;
@PersistenceContext
private EntityManager em;
public MatchDaoImpl() {
super(MatchEntity.class);
}
@Override
public List<MatchEntity> findAllWithPlayersAndTeams() {
return em.createQuery("select m from " + entityClass.getName() + " m join fetch m.teams", entityClass).getResultList();
}
}
......@@ -40,6 +40,11 @@ public class MatchFacadeImpl implements MatchFacade {
return beanMappingService.mapTo(matchService.findAll(), MatchDTO.class);
}
@Override
public List<MatchDTO> findAllMatchesWithPlayersAndTeams() {
return beanMappingService.mapTo(matchService.findAllWithPlayersAndTeams(), MatchDTO.class);
}
@Override
public MatchDTO getMatchById(String uuid) {
return beanMappingService.mapTo(matchService.findById(uuid), MatchDTO.class);
......@@ -64,7 +69,9 @@ public class MatchFacadeImpl implements MatchFacade {
var mappedTeams = new HashSet<TeamEntity>();
for (var teamID : matchDTO.getTeamsIds()) {
mappedTeams.add(teamService.findById(teamID));
var team = teamService.findById(teamID);
team.getMatches().add(match);
mappedTeams.add(team);
}
match.setTeams(mappedTeams);
match.setTournament(tournamentService.findById(matchDTO.getTournamentId()));
......
......@@ -14,6 +14,8 @@ public interface MatchService {
List<MatchEntity> findAll();
List<MatchEntity> findAllWithPlayersAndTeams();
MatchEntity findById(String id);
String update(MatchEntity match);
......
......@@ -32,6 +32,11 @@ public class MatchServiceImpl implements MatchService {
return matchDao.findAll();
}
@Override
public List<MatchEntity> findAllWithPlayersAndTeams() {
return matchDao.findAllWithPlayersAndTeams();
}
@Override
public MatchEntity findById(String id) {
if (id == null) {
......
......@@ -48,7 +48,7 @@ public class MatchController {
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
List<MatchDTO> matchDTOList = matchFacade.findAllMatches();
List<MatchDTO> matchDTOList = matchFacade.findAllMatchesWithPlayersAndTeams();
model.addAttribute("matches", matchDTOList);
return "/match/list";
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment