Java SpringBoot JPA无法保存对象
我有一个API,希望在修改后保存一个hotel对象。hotel对象的结构与另一个customer对象完全相同,但是当我尝试获取并保存hotel对象时,springboot抛出了一个属性引用异常。我的客户对象工作得非常好,所以我不确定有什么不同。我已经尝试将hotel类重命名为“Hotels”和“hotel”,但没有成功。 这就是错误:Java SpringBoot JPA无法保存对象,java,mongodb,spring-boot,jpa,Java,Mongodb,Spring Boot,Jpa,我有一个API,希望在修改后保存一个hotel对象。hotel对象的结构与另一个customer对象完全相同,但是当我尝试获取并保存hotel对象时,springboot抛出了一个属性引用异常。我的客户对象工作得非常好,所以我不确定有什么不同。我已经尝试将hotel类重命名为“Hotels”和“hotel”,但没有成功。 这就是错误: 2020-10-13 22:42:02.828 ERROR 15044 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[disp
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;
}
}
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;
}
}
}
包rc.springbootmongodbdemo.DAO;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
导入rc.springbootmongodbdemo.Entities.hotel;
导入rc.springbootmongodbdemo.Entities.hsideservation;
导入java.util.Collection;
导入java.util.Optional;
@组成部分
公务舱酒店{
@自动连线
私人酒店资源库;
createHotel公共酒店(新酒店){
返回存储库。插入(新酒店);
}
公共集合酒店预订(国际酒店id,HSIDeservation意向预订){
可选hotel=repository.findById(hotel_id);
if(hotel.isPresent()){
hotel.get().deleteReservation(意向预订);
repository.save(hotel.get());
return hotel.get().getHotelSideReservations();
}
否则{
System.out.println(“酒店未注册”);
返回null;
}
}
公共收藏新酒店(国际酒店id,HSIReservation新预订){
可选thishhotel=repository.findById(hotel_id);
if(thishhotel.isPresent()){
System.out.println(thishhotel.get().getHotelSideReservations().size());
thishhotel.get().addReservation(newReservation);
System.out.println(thishhotel.get().getHotelSideReservations().size());
//这就是问题的根源
repository.save(thishhotel.get());
返回此酒店。get().getHotelSideReservations();
}
否则返回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();
}
}
包rc.springbootmongodbdemo.Entities;
导入org.springframework.data.annotation.Id;
导入org.springframework.data.mongodb.core.mapping.Document;
导入java.util.Collection;
导入java.util.List;
公务舱酒店{
@身份证
私人国际酒店;
私人城市;
私人名单酒店预订;
公共字符串getCity(){
回归城市;
}
公共国际酒店{
返回酒店;
}
公共集合getHotelSideReservations(){
返回酒店预订部;
}
公共集合添加保留(HSIDERERVERATION NEWSERATION){
hotelSideReservations.add(新建预订);
返回酒店预订部;
}
公共收藏删除保留(hsideReservation intenderes){
对于(int i=0;i
客户对象:
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();
}
}
包rc.springbootmongodbdemo.Entities;
导入org.springframework.data.annotation.Id;
导入java.util.Collection;
导入java.util.List;
公共类客户{
@身份证
私有int-id;
私人名单客户保留;
公共整数getId(){
返回id;
}
公共集合getCustomerReservations(){
返回customerReservations;
}
公共收藏预订(酒店预订){
customerReservations.add(预订);
返回customerReservations;
}
公共收藏更新服务(酒店预订编辑){
对于(int i=0;i
客户道对象:
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.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;
}
}
}
包rc.springbootmongodbdemo.DAO;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
导入org.springframework.web.bind.annotation.PathVariable;
导入rc.springbootmongodbdemo.Entities.Customer;
导入rc.springbootmongodbdemo.Entities.hotel_预订;
导入java.util.array;
导入java.util.Collection;
导入java.util.List;
导入java.util.Optional;
@组成部分
公共类客户道{
@自动连线
私有客户存储库;
//客户预订清单
公共集合getReservations(@PathVariable(“id”)int-id){
可选客户=repository.findById(id);
if(customer.isPresent()){
返回customer.get().getCustomerReservations();
}
否则{
System.out.println(“客户未注册”)