[解决方案]:Spring boot

时间:2018-04-23 12:42:21

标签: java spring-boot spring-data-jpa

GuestRepository.java

public interface GuestRepository extends PagingAndSortingRepository<Guest,Integer> 
{
        Guest findOne(int id);
}

ReservationService.java

import java.util.*;

@Service
public class ReservationService {

    private RoomRepository roomRepository;
    private GuestRepository guestRepository;
    private ReservationRepository reservationRepository;

    @Autowired
    public ReservationService(RoomRepository roomRepository, GuestRepository guestRepository, ReservationRepository reservationRepository) {
        this.roomRepository = roomRepository;
        this.guestRepository = guestRepository;
        this.reservationRepository = reservationRepository;
    }

    public List<RoomReservation> getRoomReservationForDate(Date date){
        Iterable<Rooms> rooms=this.roomRepository.findAll();
        Map<Integer,RoomReservation> roomReservationMap=new HashMap<>();
        rooms.forEach(room->{
            RoomReservation roomReservation=new RoomReservation();
            roomReservation.setRoomId(room.getId());
            roomReservation.setFirstName(room.getName());
            roomReservation.setRoomNumber(room.getNumber());
            roomReservationMap.put(room.getId(),roomReservation);
        });

        Iterable<Reservation> reservations=this.reservationRepository.findByDate(new java.sql.Date(date.getTime()));
        if(reservations==null){
            reservations.forEach(reservation -> {
                Guest guest=this.guestRepository.findOne(reservation.getGuestId());
                if(guest==null){
                    RoomReservation roomReservation=roomReservationMap.get(reservation.getReservationId());
                    roomReservation.setDate(date);
                    roomReservation.setFirstName(guest.getFirstName());
                    roomReservation.setLastName(guest.getLastName());
                    roomReservation.setGuestId(guest.getGuest_id());
                }
            });
        }
        List<RoomReservation> roomReservations=new ArrayList<>();
        for (Integer roomId:roomReservationMap.keySet()){
            roomReservations.add(roomReservationMap.get(roomId));
        }
        return roomReservations;


    }

}

ReservationApplication.java

@SpringBootApplication
public class ReservationsApplication {

public static void main(String[] args) {
    ApplicationContext applicationContext=SpringApplication.run(ReservationsApplication.class, args);
    String[] beanNames=applicationContext.getBeanDefinitionNames();
    Arrays.sort(beanNames);
    for (String name:beanNames) {
        System.out.println(name);
    }
}
}

错误

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为&#39; reservationService&#39;的bean时出错在文件中定义   [/Users/ashlin/Downloads/reservations/target/classes/com/ashlinkarkada/reservations/business/service/ReservationService.class]:   通过构造函数参数1表示的不满意的依赖性;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; guestRepository&#39;的bean:调用init方法   失败;嵌套异常是java.lang.IllegalArgumentException:失败   为方法公共抽象创建查询   com.ashlinkarkada.reservations.data.entities.Guest   com.ashlinkarkada.reservations.data.repository.GuestRepository.findOne(INT)!   找不到类型Guest的找到的属性find!

解决方案:

public interface GuestRepository extends JpaRepository<Guest,Integer> {
@Override
Guest getOne(Integer integer);
}

0 个答案:

没有答案