【发布时间】:2018-04-24 00:26:52
【问题描述】:
我在 Repository interfase 和 DatabaseSeeder 中遇到了一些泛型问题 当我启动服务器时出现错误“推断的类型不符合上限”并且 hotelRepository.save(bookings) 下划线。有人可以帮我解决这个问题吗?
实体
@Entity
public class HotelBooking {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String hotelName;
private double pricePerNight;
private int nbOfNights;
public HotelBooking(String hotelName, double pricePerNight, int nbOfNights){
this.hotelName = hotelName;
this.pricePerNight = pricePerNight;
this.nbOfNights = nbOfNights;
}
public HotelBooking() {
}
public String getHotelName() {
return hotelName;
}
public double getPricePerNight() {
return pricePerNight;
}
public int getNbOfNights() {
return nbOfNights;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public void setPricePerNight(double pricePerNight) {
this.pricePerNight = pricePerNight;
}
public void setNbOfNights(int nbOfNights) {
this.nbOfNights = nbOfNights;
}
public long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
控制器
@RestController
@RequestMapping(value = "/bookings")
public class HotelController {
HotelRepository hotelRepository;
@Autowired
public HotelController(HotelRepository hotelRepository){
this.hotelRepository = hotelRepository;
}
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<HotelBooking> getAll(){
return hotelRepository.findAll();
}
@RequestMapping(value = "/affordable/{price}", method = RequestMethod.GET)
public List<HotelBooking> getAffordable(@PathVariable double price){
return hotelRepository.findByPricePerNightLessThan(price);
}
@RequestMapping(value ="/create", method = RequestMethod.POST)
public List<HotelBooking> create(@RequestBody HotelBooking hotelBooking){
hotelRepository.save(hotelBooking);
return hotelRepository.findAll();
}
@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
public List<HotelBooking> remove(@PathVariable Long id){
hotelRepository.deleteById(id);
return hotelRepository.findAll();
}
}
组件类
@Component
public class DatabaseSeeder implements CommandLineRunner {
private HotelRepository hotelRepository;
@Autowired
public DatabaseSeeder(HotelRepository hotelRepository){
this.hotelRepository = hotelRepository;
}
@Override
public void run(String... strings) throws Exception {
List<HotelBooking> bookings = new ArrayList<>();
bookings.add(new HotelBooking("Marriot", 200.5, 3));
bookings.add(new HotelBooking("Ibis", 300.5, 4));
bookings.add(new HotelBooking("Novotel", 100.5, 1));
hotelRepository.save(bookings);
}
}
存储库类
@Repository
public interface HotelRepository extends JpaRepository<HotelBooking, Long> {
List<HotelBooking> findByPricePerNightLessThan(double price);
}
【问题讨论】:
标签: java spring-boot spring-data-jpa