SpringBoot JPA无法保存对象。

huangapple go评论73阅读模式
英文:

SpringBoot JPA can not save object

问题

这是错误信息:

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

HotelDAO 对象:

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;
    }
}

Hotel 对象:

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();
    }
}

Customer 对象:

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();
   }
}

CustomerDAO 对象:

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;

    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;
        }
    }
}
英文:

I have an API that wants to save a hotel object after a modification. The hotel object is structured exactly the same as another customer object, however when I try to get and save a hotel object springboot is throwing a property reference exception. My customer object works perfectly fine so I am not sure what is different. I have already tried to rename the hotel class to "Hotels" and "Hotel", and it did not work.
This is the error:

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

HotelDAO Object:

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&lt;hsideReservation&gt; hotelDeleteReservation(int hotel_id, hsideReservation intendedReservation){
Optional&lt;hotel&gt; hotel = repository.findById(hotel_id);
if(hotel.isPresent()){
hotel.get().deleteReservation(intendedReservation);
repository.save(hotel.get());
return hotel.get().getHotelSideReservations();
}
else{
System.out.println(&quot;Hotel is not registered&quot;);
return null;
}
}
public Collection&lt;hsideReservation&gt; newResforHotel(int hotel_id, hsideReservation newReservation){
Optional&lt;hotel&gt; 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;
}
}

Hotel Object

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&lt;hsideReservation&gt; hotelSideReservations;
public String getCity(){
return city;
}
public int getHotel_id(){
return hotel_id;
}
public Collection&lt;hsideReservation&gt; getHotelSideReservations(){
return hotelSideReservations;
}
public Collection&lt;hsideReservation&gt; addReservation(hsideReservation newReservation){
hotelSideReservations.add(newReservation);
return hotelSideReservations;
}
public Collection&lt;hsideReservation&gt; deleteReservation(hsideReservation intendedRes) {
for(int i = 0; i &lt; 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(&quot; , &quot;);
}
return s.toString();
}
}

Customer Object:

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&lt;hotel_reservations&gt; customerReservations;
public Integer getId(){
return id;
}
public Collection&lt;hotel_reservations&gt; getCustomerReservations(){
return customerReservations;
}
public Collection&lt;hotel_reservations&gt; makeReservation(hotel_reservations reservation){
customerReservations.add(reservation);
return customerReservations;
}
public Collection&lt;hotel_reservations&gt; updateNewReservation(hotel_reservations editedRes){
for(int i = 0; i &lt; customerReservations.size(); i++){
if(customerReservations.get(i).getReservation_id() == editedRes.getReservation_id()){
customerReservations.get(i).set(editedRes);
return customerReservations;
}
}
return customerReservations;
}
public Collection&lt;hotel_reservations&gt; removeReservation(hotel_reservations intendedRes){
for(int i = 0; i &lt; 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(&quot; , &quot;);
}
return s.toString();
}
}

CustomerDAO Object:

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&lt;hotel_reservations&gt; getReservations(@PathVariable(&quot;id&quot;) int id){
Optional&lt;Customer&gt; customer = repository.findById(id);
if(customer.isPresent()){
return customer.get().getCustomerReservations();
}
else{
System.out.println(&quot;Customer is not registered&quot;);
return null;
}
}
public Customer createCustomer(Customer newCustomer) {
return repository.insert(newCustomer);
}
public Collection&lt;hotel_reservations&gt; customerUpdateReservation(int id, hotel_reservations newReservation){
Optional&lt;Customer&gt; customer = repository.findById(id);
if(customer.isPresent()){
customer.get().updateNewReservation(newReservation);
repository.save(customer.get());
return customer.get().getCustomerReservations();
}
else{
System.out.println(&quot;Customer is not registered&quot;);
return null;
}
}
public Collection&lt;hotel_reservations&gt; createNewCustomerReservation(int customerId, hotel_reservations newReservation) {
Optional&lt;Customer&gt; customer = repository.findById(customerId);
if(customer.isPresent()){
customer.get().makeReservation(newReservation);
repository.save(customer.get());
return customer.get().getCustomerReservations();
}
else{
System.out.println(&quot;Customer is not registered&quot;);
return null;
}
}
public Collection&lt;hotel_reservations&gt; customerDeleteReservation(int id, hotel_reservations newReservation){
Optional&lt;Customer&gt; customer = repository.findById(id);
if(customer.isPresent()){
customer.get().removeReservation(newReservation);
repository.save(customer.get());
return customer.get().getCustomerReservations();
}
else{
System.out.println(&quot;Customer is not registered&quot;);
return null;
}
}
}

答案1

得分: 0

Spring在实现时使用标准命名约定。

请纠正以下内容并检查是否解决。

  1. 将类名hotel重命名为Hotel

  2. 将hotel_id重命名为hotel_id

  3. 将hotel_reservations重命名为HotelReservations

使用Java标准命名约定以避免任何异常问题。

英文:

Spring uses standard naming conventions when it comes to implementation.

Please correct below things and check whether it gets resolved.

  1. Rename class name hotel to Hotel

  2. Rename hotel_id to hotel is

  3. Rename hotel_reservations to HotelReservations

Use java standard naming conventions to avoid any abnormal issues.

huangapple
  • 本文由 发表于 2020年10月14日 10:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64345978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定