推断类型不符合上限 hotelRepository.save(bookings) 下划线 [英] inferred type does not conform to upper bound(s) hotelRepository.save(bookings) underlined
问题描述
我在 Repository 接口和 DatabaseSeeder 中遇到了一些泛型问题当我启动服务器时出现错误推断的类型不符合上限"和带下划线的hotelRepository.save(bookings).有人可以帮我解决这个问题吗?
I have some problems with generics in Repository interfase and in DatabaseSeeder When I launch server appears error "inferred type does not conform to upper bound(s)" and hotelRepository.save(bookings) underlined. Could somebody help me to solve tis issue?
实体
@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);
}
推荐答案
只是对上一个答案的一个小补充:如果您使用 spring-boot 1.5,您的代码应该可以工作,但在 2.0 中,spring 数据 CrudRepository 的 API 的实现已更改,现在您应该使用 saveAll 来保存可迭代对象.
Just a small addition the the previous answer: Your code should work if you use spring-boot 1.5 but in 2.0 the implementation of the API of the spring data CrudRepository is changed and now you should use saveAll to save iterables.
如果您想在一个 saveAll 请求中保存更多数据,您还应该考虑配置批量请求.https://vladmihalcea.com/the-best-way-to-do-batch-processing-with-jpa-and-hibernate/
You should also consider configuring your batch requests if you want to save a greater amount of data in one saveAll request. https://vladmihalcea.com/the-best-way-to-do-batch-processing-with-jpa-and-hibernate/
这篇关于推断类型不符合上限 hotelRepository.save(bookings) 下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!