【问题标题】:inferred type does not conform to upper bound(s) hotelRepository.save(bookings) underlined推断类型不符合上限 hotelRepository.save(bookings) 下划线
【发布时间】: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


    【解决方案1】:

    您应该使用hotelRepository.saveAll(bookings); 而不是hotelRepository.save(bookings);

    【讨论】:

    • 非常感谢您的帮助,请您解释一下,在什么情况下我可以使用 save() ?
    • save() 方法是保存一个实体的单个实例。
    • K. Siva Prasad Reddy 非常感谢您的建议对我有帮助
    【解决方案2】:

    只是对上一个答案的一个小补充: 如果您使用 spring-boot 1.5,您的代码应该可以工作,但在 2.0 中,spring 数据 CrudRepository 的 API 的实现已更改,现在您应该使用 saveAll 来保存可迭代对象。

    如果您想在一个 saveAll 请求中保存更多数据,您还应该考虑配置批处理请求。 https://vladmihalcea.com/the-best-way-to-do-batch-processing-with-jpa-and-hibernate/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2020-05-04
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      相关资源
      最近更新 更多