Skip to content
Snippets Groups Projects
Commit 7d85d68b authored by Martin Slovík's avatar Martin Slovík
Browse files

Fixing saving of Steward to a Flight assignment through custom connection...

Fixing saving of Steward to a Flight assignment through custom connection table. Adding eager fetching method.
parent bb4d7ca4
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,8 @@ import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.Objects;
@Entity(name = "flights")
@Entity
@Table(name = "flights")
@Data
@NoArgsConstructor
public class Flight extends DomainEntity {
......
......@@ -2,7 +2,8 @@ package cz.muni.fi.pa165.core.data.repository.flight;
import cz.muni.fi.pa165.core.data.domain.Flight;
import cz.muni.fi.pa165.core.data.repository.common.BaseRepository;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;
......@@ -10,7 +11,11 @@ import java.util.Optional;
@Repository
public interface FlightRepository extends BaseRepository<Flight, Long> {
@Override
@EntityGraph(attributePaths = {"flightStewards"})
Optional<Flight> findById(Long id);
/**
* Returns Flight entity with eagerly fetched Stewards
* @param id flightId
* @return Optional<Flight>
*/
@Query("SELECT f FROM Flight f JOIN FETCH f.flightStewards fs WHERE f.id = :id")
Optional<Flight> findByIdWithStewards(@Param("id") Long id);
}
package cz.muni.fi.pa165.core.data.repository.flightsteward;
import cz.muni.fi.pa165.core.data.domain.FlightSteward;
import cz.muni.fi.pa165.core.data.repository.common.BaseRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FlightStewardRepository extends BaseRepository<FlightSteward, Long> {
}
......@@ -70,12 +70,10 @@ public class StewardFacadeImpl implements StewardFacade<Long> {
var flightSteward = new FlightSteward();
flightSteward.setSteward(stewardEntity);
flightSteward.setFlight(flightEntity);
stewardService.saveFlightStewards(flightSteward);
flightEntity.getFlightStewards().add(flightSteward);
flightService.save(flightEntity);
stewardEntity.getFlightStewards().add(flightSteward);
stewardService.save(stewardEntity);
// Check that Flight members were eagerly fetched
var flightEntityOptEagerlyFetched = flightService.findByIdWithStewards(flightId);
//TODO create DTO without nested members
return null;
......
......@@ -3,6 +3,9 @@ package cz.muni.fi.pa165.core.service.flight;
import cz.muni.fi.pa165.core.data.domain.Flight;
import cz.muni.fi.pa165.core.service.common.BaseService;
import java.util.Optional;
public interface FlightService extends BaseService<Flight, Long> {
Optional<Flight> findByIdWithStewards(Long id);
}
......@@ -6,6 +6,8 @@ import cz.muni.fi.pa165.core.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class FlightServiceImpl extends BaseServiceImpl<Flight, Long> implements FlightService {
......@@ -16,4 +18,9 @@ public class FlightServiceImpl extends BaseServiceImpl<Flight, Long> implements
super(flightRepository);
this.flightRepository = flightRepository;
}
@Override
public Optional<Flight> findByIdWithStewards(Long id) {
return flightRepository.findByIdWithStewards(id);
}
}
package cz.muni.fi.pa165.core.service.steward;
import cz.muni.fi.pa165.core.data.domain.FlightSteward;
import cz.muni.fi.pa165.core.data.domain.Steward;
import cz.muni.fi.pa165.core.service.common.BaseService;
public interface StewardService extends BaseService<Steward, Long> {
/**
* Saves the assignment of Steward to a Flight
* @param flightSteward Flight<->Steward connection table
* @return FlightSteward connection table stored in DB
*/
FlightSteward saveFlightStewards(FlightSteward flightSteward);
}
package cz.muni.fi.pa165.core.service.steward;
import cz.muni.fi.pa165.core.data.domain.FlightSteward;
import cz.muni.fi.pa165.core.data.domain.Steward;
import cz.muni.fi.pa165.core.data.repository.flightsteward.FlightStewardRepository;
import cz.muni.fi.pa165.core.data.repository.steward.StewardRepository;
import cz.muni.fi.pa165.core.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -10,10 +12,17 @@ import org.springframework.stereotype.Service;
public class StewardServiceImpl extends BaseServiceImpl<Steward, Long> implements StewardService {
private final StewardRepository stewardRepository;
private final FlightStewardRepository flightStewardRepository;
@Autowired
public StewardServiceImpl(StewardRepository stewardRepository) {
public StewardServiceImpl(StewardRepository stewardRepository, FlightStewardRepository flightStewardRepository) {
super(stewardRepository);
this.stewardRepository = stewardRepository;
this.flightStewardRepository = flightStewardRepository;
}
@Override
public FlightSteward saveFlightStewards(FlightSteward flightSteward) {
return flightStewardRepository.save(flightSteward);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment