当前位置: 首页 > 知识库问答 >
问题:

SpringBoot JPA无法保存对象

邢凌
2023-03-14
2020-10-13 22:42:02.828 ERROR 15044 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property hotel found for type hotel!] with root cause

package rc.springbootmongodbdemo.DAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rc.springbootmongodbdemo.Entities.hotel;
import rc.springbootmongodbdemo.Entities.hsideReservation;

import java.util.Collection;
import java.util.Optional;

@Component
public class HotelDAO {
    @Autowired
    private HotelRepository repository;

    public hotel createHotel(hotel newHotel) {
        return repository.insert(newHotel);
    }

    public Collection<hsideReservation> hotelDeleteReservation(int hotel_id, hsideReservation intendedReservation){
        Optional<hotel> hotel = repository.findById(hotel_id);
        if(hotel.isPresent()){
            hotel.get().deleteReservation(intendedReservation);
            repository.save(hotel.get());
            return hotel.get().getHotelSideReservations();
        }
        else{
            System.out.println("Hotel is not registered");
            return null;
        }
    }

    public Collection<hsideReservation> newResforHotel(int hotel_id, hsideReservation newReservation){
        Optional<hotel> thisHotel = repository.findById(hotel_id);
        if(thisHotel.isPresent()){
            System.out.println(thisHotel.get().getHotelSideReservations().size());
            thisHotel.get().addReservation(newReservation);
            System.out.println(thisHotel.get().getHotelSideReservations().size());
            //This is where the issue originates
            repository.save(thisHotel.get());
            return thisHotel.get().getHotelSideReservations();
        }
        else return null;
    }

}
package rc.springbootmongodbdemo.Entities;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Collection;
import java.util.List;
public class hotel {
    @Id
    private int hotel_id;
    private String city;
    private List<hsideReservation> hotelSideReservations;

    public String getCity(){
        return city;
    }
    public int getHotel_id(){
        return hotel_id;
    }
    public Collection<hsideReservation> getHotelSideReservations(){
        return hotelSideReservations;
    }
    public Collection<hsideReservation> addReservation(hsideReservation newReservation){
        hotelSideReservations.add(newReservation);
        return hotelSideReservations;
    }


    public Collection<hsideReservation> deleteReservation(hsideReservation intendedRes) {
        for(int i = 0; i < hotelSideReservations.size(); i++){
            if(hotelSideReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
                hotelSideReservations.remove(i);
                return hotelSideReservations;
            }
        }
        return hotelSideReservations;
    }
    public String toString(){
        StringBuilder s = new StringBuilder();
        for(hsideReservation h : hotelSideReservations) {
            s.append(h.getDate());
            s.append(" , ");
        }
        return s.toString();
    }

}


package rc.springbootmongodbdemo.Entities;

import org.springframework.data.annotation.Id;

import java.util.Collection;
import java.util.List;

public class Customer {
    @Id
    private int id;
    private List<hotel_reservations> customerReservations;

    public Integer getId(){
        return id;
    }
    public Collection<hotel_reservations> getCustomerReservations(){
        return customerReservations;
    }

    public Collection<hotel_reservations> makeReservation(hotel_reservations reservation){
        customerReservations.add(reservation);
        return customerReservations;
    }

    public Collection<hotel_reservations> updateNewReservation(hotel_reservations editedRes){
        for(int i = 0; i < customerReservations.size(); i++){
            if(customerReservations.get(i).getReservation_id() == editedRes.getReservation_id()){
                customerReservations.get(i).set(editedRes);
                return customerReservations;
            }
        }
        return customerReservations;
        }

    public Collection<hotel_reservations> removeReservation(hotel_reservations intendedRes){
        for(int i = 0; i < customerReservations.size(); i++){
            if(customerReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
                customerReservations.remove(i);
                return customerReservations;
            }
        }
        return customerReservations;
    }

   public String toString(){
        StringBuilder s = new StringBuilder();
        for(hotel_reservations h : customerReservations) {
            s.append(h.getDate());
            s.append(" , ");
        }
        return s.toString();
   }
}

package rc.springbootmongodbdemo.DAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import rc.springbootmongodbdemo.Entities.Customer;
import rc.springbootmongodbdemo.Entities.hotel_reservations;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;


@Component
public class CustomerDAO {
    @Autowired
    private CustomerRepository repository;
    //List of customer reservations
    public Collection<hotel_reservations> getReservations(@PathVariable("id") int id){
        Optional<Customer> customer = repository.findById(id);
        if(customer.isPresent()){
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }

    }

    public Customer createCustomer(Customer newCustomer) {
        return repository.insert(newCustomer);
    }

    public Collection<hotel_reservations> customerUpdateReservation(int id, hotel_reservations newReservation){
        Optional<Customer> customer = repository.findById(id);
        if(customer.isPresent()){
            customer.get().updateNewReservation(newReservation);
            repository.save(customer.get());
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }
    }

    public Collection<hotel_reservations> createNewCustomerReservation(int customerId, hotel_reservations newReservation) {
        Optional<Customer> customer = repository.findById(customerId);
        if(customer.isPresent()){
            customer.get().makeReservation(newReservation);
            repository.save(customer.get());
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }
    }
    public Collection<hotel_reservations> customerDeleteReservation(int id, hotel_reservations newReservation){
        Optional<Customer> customer = repository.findById(id);
        if(customer.isPresent()){
            customer.get().removeReservation(newReservation);
            repository.save(customer.get());
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }
    }
}

暂时还没有答案

 类似资料:
  • 我试图对模型的对象调用方法来更新数据库中的特定资源,但当我在api url上发送POST请求时,会出现此错误 方法照明\数据库\雄辩\集合::保存不存在。 我也尝试调用方法,但同样的错误。 编辑代码: 第一行返回响应和正确响应。 和 我还试图调用或方法来删除资源,但它也会给出相同的错误 BadMethodCallException:方法Illumb\Database\Eloquent\Collec

  • 我正对着< code > org . Apache . poi . open XML 4j . exceptions . open XML 4 jruntimeexception:保存失败:保存包时出错:无法用marshaller在流中保存零件/docProps/app.xml 在每个测试场景执行完成后,尝试将每个测试场景结果(通过或失败)写入Excel表(.xlsx)时出现异常。为此,我编写了以

  • 问题内容: 我有一些与关系有关的实体: 和 使用某些模型执行保存模型时,一切正常。表存储这些实体的所有键。但是,使用驱动程序保存模型时,表不会更改。我认为映射存在问题。 问题答案: 那是预期的行为。在双向多对多关联中,一侧必须是反侧。在您的情况下,这是一面,因为它包含: 拥有关系的字段。除非关系是单向的,否则为必需。 这意味着是关联的所有者,并且Hibernate仅在维护关联时检查该侧。

  • 以前,我是这个AutoIT的新手,我不熟悉这个,只是谷歌需要完成我的任务,如果我的AtotoIt有任何错误,请更正它 我的autoIT代码(参考自http://qtp-help.blogspot.in/2009/07/selenium-handle-dialogs.html#handle_save_dialog) 我的java代码 请帮忙解决